views:

177

answers:

3

Hi Guys,

I'm using the S#arp architecture, and I can't remember where I read it, but they say they ViewModels should be stored at the service layer, and your views should submit the viewmodel to the service for processing.

My question then is this. Which layer should construct the ViewModel? Should it be at the service layer, and the controller requests it? Or should the controller construct it itself? There is also a question about updating the view model, as if it contains collections, and the model state is invalid, you will also need to repopuplate any lists.

Any suggestions?

Many thanks

Matt

+3  A: 

I create view models inside controllers. Controllers take domain entities (retrieved from database by model binders), possibly inside other view models, contact repositories for additional data, create new view model, and pass it to appropriate view (or redirect). So controllers responsibility is to prepare view/viewmodel according to input domain data (and handle errors of course).

You can look here for alternative to creating view models in controller. This technique moves view model creation outside actions, so that not only controller actions accept pure domain objects, but they also return pure domain objects. I wouldn't say it's appropriate in all cases, but it's very interesting to learn.

The above technique, related to AutoMapper, also raised questions similar to "should I pass viewmodels to service layer". No you don't. If you need to pass complex object to service or domain layer, you define this object in the appropriate service/domain layer and use it to pass data to those layers. This object then can be easily mapped to/from view models (for example, using AutoMapper). But your lower layers (service/domain) should not be coupled to upper layers (view/controllers). Not in this case, not in others. Never low level layers should depend on something defined above them.

queen3
Thats pretty much what I am doing now, but I must be doing something wrong as I seem to have a lot of conditional logic when minding the view model back to the domain. Maybe I need to break my view up into smaller chunks.
Matt Brailsford
I currently have an edit view which addapts based on the status of the entity. Would I be better off creating multiple views for the different states?
Matt Brailsford
Without seeing your edit view and models, this is hard to answer.
queen3
+1  A: 

These articles might be interesting to you:

DDD : Command Query Separation as an Architectural Concept

Better Application Services and CQS using S#arp Architecture

There is a sample application associated with the 2nd article that has the view and form models in a services layer, instead of the controller.

Jason Watts
This is the correct answer. I do, however, usually start with the ViewModel in the controller and migrate it to the service layer as the controller evolves.
chum of chance
A: 

also look at Who Can Help Me -- it's fantastic. this framework is based on S#arp Architecture. it has lots of guidance for View/Form/Edit viewModels amongst other things.

CurlyFro