Should the service layer accept objects or an identifier as input? Should responses be objects?

I have a pretty familiar design, where I have a DAO for each model, and then a class of the Model class, which uses DAO and has all the business logic.

I am a little confused as to how to correctly create these classes of service, specifically what my parameters and my answers should be.

Let's say that I have a JSON action, I can read JSON and then load the objects I need and then pass them to the service, or I can just pass the identifier and then load the objects in the service method.

The embarrassment is that I could do some of these things in the operation of my controllers, or I could do this at my service level. Is there a clear answer to these questions?

pseudo code below:

UserDao
  save
  update
  delete
  getById

UserService
  private userDao

  GetUser
  Update

I will use my service level in the BOTH web application and API level.

I could have a way like this:

def GetSomething(userId: Int, locationId: Int, ...): Something = { ... }

or I could do this:

def GetSomething(request: GetSomethingRequest): GetSomethingResponse { ... }

Is this one or the other, or should I mix both?

+4
source share
1 answer

At first, I think that using case classes for identifiers is a good description, so use UserId(id: Int)and LocationId(id: Int) Let's say you have a model like this:User(id: UserId, locations: Set[Location])

, , /user/1/location/1 , Location json. 1. User id ( → NotFound ( id)) 2. User Location ( → NotFound (location with id)

, : def find(userId: UserId):Option[User] = {...}. Your User : def findLocation (id: LocationId): Option [Location]. findLocation User " "

. , Location , (: , ..). , : LocationData ( , json ..) def update(data: LocationData): Location Location, .

0

Source: https://habr.com/ru/post/1614379/


All Articles