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

前端 未结 2 1415
误落风尘
误落风尘 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: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.

提交回复
热议问题