Should a service layer return view models for an MVC application?

后端 未结 6 1759
时光说笑
时光说笑 2020-12-04 12:52

Say you have an ASP.NET MVC project and are using a service layer, such as in this contact manager tutorial on the asp.net site: http://www.asp.net/mvc/tutorials/iteration-

6条回答
  •  自闭症患者
    2020-12-04 13:23

    Generally, no.

    View models are intended to provide information to and from views and should be specific to the application, as opposed to the general domain. Controllers should orchestrate interaction with repositories, services (I am making some assumptions of the definition of service here), etc and handle building and validating view models, and also contain the logic of determining views to render.

    By leaking view models into a "service" layer, you are blurring your layers and now have possible application and presentation specific mixed in with what should focused with domain-level responsibilities.

提交回复
热议问题