Bestpractice - Mixing View Model with Domain Model
Asked Answered
H

2

10

Is it reasonable to mix view models with domain models?

So i.e. the view model object contains some domain model objects (not the other way around!)

Holley answered 22/6, 2010 at 15:31 Comment(0)
G
9

Generally, you will have to reference your Domain Models in your View Models, or at least load the Domain Models in the controllers and pass the information on to your View Model.

I prefer to keep Controllers and Views as simple/dumb as possible, because both Domain Models and View Models are FAR easier to test.

So, I often reference my Domain Models inside my View Models. Sometimes I use aggregation, sometimes I just copy over properties (In some projects just with plain old code, in other projects using an auto mapper)

Gelsemium answered 22/6, 2010 at 15:36 Comment(7)
but doing this is only reasonable and does only work fine when your domain model suits the needs of the view well. I heard that a view model should only nearly contain string properties that are already formated. Btw you DO NOT have to copy properties. I suggest you reading about AutoMapper, just google it! It's a great tool imho.Holley
Note I said in some projects I copy, in others I use an auto mapper, specifically, AutoMapper :) In addition, I do basically what you describe. I do NOT use my Domain Models as my View Models. We used to do this and it bit us quite a few times.Gelsemium
So as I understand you right, you mix view model objects and domain model objects. And you send both (i.e. the aggregate containing a view model object and domain model object) to the view as well?Holley
Basically, yes. The View Model is made to serve the view exactly. Sometimes I send the whole Domain Object along, sometimes I only send the bits the View needs. Whatever seems to make the most sense given the demands of the View.Gelsemium
Ok. Does your approach violate the rule of "Seperation of Concerns"?Holley
I'd guess in a purist sense, yes. But at some point, you have to hand off the Domain Model data to the View Model data. So somewhere they have to know about each other :) I'd guess to be 100% pure you'd want to do it all in the Controller, but for me, down that road lied madness :)Gelsemium
I agree with @Rookian, the viewmodel shouldn't hold a reference to the domain model, it's not part of its responsibility and it should not change in case the domain model changes. Your controller should call auto mapper or some extension method which will map from domain model to the plain view model.Maffei
F
5

I tend to create separate view models that contain just what I need to be displayed in the view. AutoMapper is a create tool for making this easier.

Fingering answered 22/6, 2010 at 15:45 Comment(3)
Could you explain more in detail why you would NOT mix view model objects with domain model objects? Pros and Cons ...Holley
My domain model classes typically have a lot of state mutator methods on them. I don't want to expose these directly to the view layer simply to prevent confusion on where those methods should be called from. I require all my domain commands to go through a service layer which wires up the appropriate DB sessions, security checks, etc. However I typically DO expose value objects from my domain straight to the MVC layer. Also having a clean separation between read vs write models (CQS) helps keep this organized.Nutriment
I try to make it so that I have a view model for each view and that view model on has what it needs to create the view. That view model could be made up of multiple domain models, and only be using some of the fields from each.Fingering

© 2022 - 2024 — McMap. All rights reserved.