Reputation: 983
I know there are plenty of opinions in this, but is still not clear for me.
Let's ask it with an example, supposing I have a Customer
entity, and a CustomerDTO
.
1) Should my save
and get
service accept a DTO and return a DTO? Like:
CustomerDTO saveNew(CustomerDTO dto);
CustomerDTO get(Long id);
2) Should my service always manage Customer
objects and my Controller
make the conversion, like:
Customer saveNew(Customer c);
Customer get(Long id);
I think the 2nd is more flexible, and then if in another service I need to call to the customer get (for set it in a User
, for example) I have the Customer
itself and I don't need to convert it. But seems that 1st approach is more decoupled?
Upvotes: 0
Views: 725
Reputation: 1148
A good MVC practice is having one controller per view, so we should be able to easily swap out views. Let's say we have two controllers handling two different representations(two different DTOs in this case) for our domain models. Those two controllers should make use of the same service, so they should pass the same object type to the service, which would be Customer
. In my opinion, the service
is part of the business logic layer and should work with domain models and the controller
is part of the representation layer. To answer your question, I agree with the second example, as it respects more the MVC pattern.
Upvotes: 2