开发者

ASP.Net MVC Architecture - Location of ViewModels

开发者 https://www.devze.com 2022-12-12 12:17 出处:网络
We have a decent sized MVC project running wel开发者_如何学编程l at the moment, i\'ve started to look at some re-factoring and I have a question.

We have a decent sized MVC project running wel开发者_如何学编程l at the moment, i've started to look at some re-factoring and I have a question.

At the moment the Data Layer and Service Layer is stored in a seperate class library. The controllers load the data objects (generated from linq2sql) from the service layer which does any logic check and then converts them to viewmodels (Using Auto-Mapper).

Instead of this should the ViewModels be returned directly from the service?


Definitely not!

A ViewModel's purpose is to mediate between the view and the 'real' data objects - it's totally view-specific. So layers other than your GUI shouldn't even know that such a model exists, if you want to keep a clean separation of concerns...


I would say not. The point of the service is that it can be used by many different projects that deal with your business layer. I would expect this to be in terms of your business objects. View models are specific to an MVC application and, thus, I would expect them to be separate from the service layer. Note that they often encompass both business and "housekeeping" data for the application, and may encapsulate multiple business objects. I think I would continue to transform them in your controller.

0

精彩评论

暂无评论...
验证码 换一张
取 消