views:

158

answers:

3

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!)

A: 

Yes, though you should expose your domain model through interfaces, not concrete classes.

fearofawhackplanet
I have NEVER seen a domain model that uses interfaces for entity types, accept of a base domain object that have a simple Id?!
Rookian
Since you obviously posses such an encyclopedic knowledge of software engineering best practices, I'm suprised you had to ask such a basic question in the first place. What's the point in asking a question if you're just going to tell everone that tries to help you that they're wrong? If you've never seen a domain model that uses interfaces, either you've only ever worked on trivially small projects, or you've never seen good code.
fearofawhackplanet
I am working in a project where view models contain domain objects. My knowledge is that you should use view models in the UI, because of keeping the view simple and in general seperating concerns. I asked myself, if it is correct to mix those models, because I am not sure if this is reasonable, but I tend to use view models only. As an unexperienced developer I started this question for getting some thoughts of other developers. I am glad to see an example of a domain model where you show some interfaces ;)
Rookian
+2  A: 

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)

CubanX
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.
Rookian
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.
CubanX
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?
Rookian
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.
CubanX
Ok. Does your approach violate the rule of "Seperation of Concerns"?
Rookian
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 :)
CubanX
A: 

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.

Robin Robinson
Could you explain more in detail why you would NOT mix view model objects with domain model objects? Pros and Cons ...
Rookian
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.
Ryan
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.
Robin Robinson