Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller -> Service -> Repository: Does service map Entity to ViewModel?

I haven MVC app, with "M" including Service and Repository layers.

However, I am a little confused as to where and how to do a couple of things.

  1. One Service calling two repositories, or calling it's own repository and another service

e.g.

I have a ReferenceDataService, which handles all of the logic for CRUD with my reference tables.

Then in my "CustomerService" I need to 'R' my reference data to get e.g. Description instead of Id. So, do I call the ReferenceDataService or ReferenceDataRepository?

  1. At some layer I'd like to map from Entity to ViewModel.

Do I do this in my Service layer, or in the Controller?

e.g. Does my ServiceLayer do the mapping/logic from VM to Entity and back?

Thanks:)

like image 521
BlueChippy Avatar asked Jul 18 '12 09:07

BlueChippy


1 Answers

  • Repositories talk to an underlying data source.
  • Service layer talks to repositories with domain models. It takes/passes domain models from/to the repository layer.
  • Controller talks to service layer. Controller takes/passes domain models from/to the service layer.
  • Controller calls mapping layer (if any) to map between the domain models and view models. If you don't have a mapping layer you could do the mapping in your controller although this could quickly become cumbersome in which case AutoMapper could serve as a very handy mapping layer.

Another more simpler scenario is when you don't need a service layer which is often the case in smaller applications. A service layer brings no benefit. So the controller talks directly to the repositories with the domain models.

like image 177
Darin Dimitrov Avatar answered Sep 16 '22 18:09

Darin Dimitrov