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

前端 未结 2 1413
误落风尘
误落风尘 2021-02-04 16:17

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 Serv
相关标签:
2条回答
  • 2021-02-04 16:41

    ViewModel contains data, required for displaying model on view. If you'll use another view (e.g. mobile application, or desktop application, or even web service) you will require another data to be displayed on view. If you'll do mappings on service layer, then you will not be able to use it with another type of application. Thus controller is a place where you map domain data to display them on view (whatever type of view you have).

    0 讨论(0)
  • 2021-02-04 16:45
    • 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.

    0 讨论(0)
提交回复
热议问题