Should services return Viewmodels?
Services should only care about the problem domain, not the view that generates the results. Return values must be expressed in terms of domain objects, not views. According to the traditional approach or theory, the ViewModel should be part of the UI layer. At least the name says so.
Table of Contents
What does the service layer do?
A service layer defines the boundary of an application [Cockburn PloP] and its set of available operations from the perspective of the client layers interface. It encapsulates the business logic of the application, controlling the transactions and coordinating the responses in the implementation of its operations.
Do you need a service layer?
5 answers. A service layer is not always needed. Especially if your APIs are just simple CRUD operations, for example, with no real logic or calculations needed. However, if you have an API that does some logic before querying your repository, then it should be in a separate service class.
What does the service layer do in MVC?
A service layer is an additional layer in an ASP.NET MVC application that mediates communication between a controller and a repository layer. The service layer contains business logic. In particular, it contains validation logic.
Should a service return a DTO?
Yes you should return DTO by your service layer as you have talked to your repository in service layer with domain model members and assign them to DTO and return to MVC controller and vice versa.
What should the repository return?
Your repositories should return domain objects and the repository client can decide if it needs to do the mapping. By assigning domain objects to view models (or something else) within a repository, you prevent your repositories client from gaining access to the underlying domain object.
Where should DTO be used?
A DTO is useful whenever you need to group values into ad hoc structures to pass data. From a pure design perspective, DTOs are a really close to perfect solution. DTOs help further decouple the presentation of the service layer and the domain model.