views:

131

answers:

2

We have a decent sized MVC project running well 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?

+2  A: 

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.

tvanfosson
This is what I thought. Thanks, i'll leave this open for other opinions.
Pino
+3  A: 

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...

Thomas Weller