LiamB
LiamB

Reputation: 18586

ASP.Net MVC Architecture - Location of ViewModels

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?

Upvotes: 3

Views: 306

Answers (2)

Thomas Weller
Thomas Weller

Reputation: 11717

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

Upvotes: 4

tvanfosson
tvanfosson

Reputation: 532465

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.

Upvotes: 2

Related Questions