DTO = ViewModel?

asp.net MvcDomain Driven-DesignViewmodelDto

asp.net Mvc Problem Overview


I'm using NHibernate to persist my domain objects. To keep things simple I'm using an ASP.NET MVC project as both my presentation layer, and my service layer.

I want to return my domain objects in XML from my controller classes. After reading some posts here on Stack Overflow I gather DTOs are the way to go. However, I've also come across posts talking about the ViewModel.

My question: Are Data Transfer Objects and ViewModels the same thing? Or is a ViewModel a kind of sub pattern of a DTO?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

The canonical definition of a DTO is the data shape of an object without any behavior.

ViewModels are the model of the view. ViewModels typically are full or partial data from one or more objects (or DTOs) plus any additional members specific to the view's behavior (methods that can be executed by the view, properties to indicate how toggle view elements etc...). You can look at the viewmodel as all the data for a view plus behaviors. ViewModels may or may not map one to one to business objects or DTOs.

By the way, NHibernate projections come in handy if a certain viewmodel needs a subset of the data from a persisted object.

Solution 2 - asp.net Mvc

ViewModel in ASP.NET MVC practice is the same as the DTO, however ViewModel in MVVM pattern is different from DTO because ViewModel in MVVM has behaviors but DTO does not have.

Solution 3 - asp.net Mvc

> DTO != ViewModel

In the MVVM pattern the ViewModel is used to isolate the Model from the View. To represent the Model you could use simple DTO classes, which again is mapped to a database through e.g. NHibernate. But I've never seen a ViewModel class which is modelled as a DTO.. ViewModel classes mostly have behavior, which DTOs don't have.

Solution 4 - asp.net Mvc

DTO - Data Transfer Objects are exactly as it says, containers for transferring data. They have no behaviour but merely a bunch of setters and getters. Some people make them immutable and just create new ones when needed rather than updating existing ones. They should be serializable to allow transfer across the wire.

Generally DTOs are used to ship data from one layer to another layer across process boundries as calls to a remote service can be expensive so all the required data is pushed into a DTO and transferred to the client in one chunk (coarse grained).

However, some people use the notion of screen bound DTOs (nothing to do with crossing process boundries). Again these are populated with the required data (generally the data required for a particular screen and could be an aggregation of data from various sources) and sent to the client.

http://blog.jpboodhoo.com/CommentView,guid,21fe23e7-e42c-48d8-8871-86e65bcc9a50.aspx

In simple cases as has already been stated this DTO can be used for binding to the view but in more complex cases it would require the creation of a ViewModel and unloading of data from DTO to ViewModel which is obviously more work (when applying MVVM pattern).

So again as already stated DTO!=ViewModel

and

DTO and ViewModel have different purposes in life

Solution 5 - asp.net Mvc

First, the major difference is that ViewModel can have behaviour or methods that DTO Must Not !!!

Second, Using DTO as a ViewModel in ASP.NET MVC make your application tightly coupled to DTO and that's exactly the opposite purpose of using DTO. If you do so, what's the difference using your domain Model or DTO, more complexity to get an anti-pattern ?

Also ViewModel in ASP.NET can use DataAnnotations for validation.

The same DTO can have different ViewModels Mapping, and One ViewModel can be composed from differents DTO (always with object mapping not composition) . because i think it is even worse if you have a ViewModel that contains a DTO, we will have the same problem.

From your presentation layer, think about DTO as a contract, you will receive an object that you have to consider as stranger to your application and don't have any control on it (even if you have ex the service, the dto and presentation layers are yours).

Finally if you do this clean separation, developpers can work together with ease. The person who design ViewModels, Views and Controllers don't have to worry about the service layer or the DTO implementation because he will make the mapping when the others developpers finish their implementation... He can even use Mocking tool or manual mocking to fill the presentation layer with data for test.

Solution 6 - asp.net Mvc

For some simple views I'll use my DTO as my models, but as Views become more complex I'll create ViewModels.

For me it is a balance between quickness (using DTO, since I already have 'em) and flexibility (creating ViewModels means more separation of concerns).

Solution 7 - asp.net Mvc

If you need to change or enhance the DTO then create a ViewModel. It's also OK for the ViewModel to reference the DTO as a complex property.

In practice, you will generally want to add view-specific properties or methods to the model you are using in the view. In such cases, never modify the DTO for your view requirements. Instead, create a ViewModel and map from your DTO to the ViewModel.

Solution 8 - asp.net Mvc

If you will use DTO as ViewModel, that means you are making high dependency on DTO because of some reason you are changing DTO then it could impact on ViewModel.

Better use DTO & convert into viewmodel.

Solution 9 - asp.net Mvc

We can use DTO same as Model class and we can use viewmodel when we needs to show/use multiple models data/property in a single view. Example: I create some model using entity framework database first. So, now all the model generate based on the database. and now we need data annotation, for those data annotation we can create a folder name DTO, In this DTO folder, we can keep all the models exact which already generate and add data annotation above the property. Then we can use any operation(use controller , views) using this DTO classes. And when we need complex view, I mean when we need multiple classes data in one view there we can use viewmodel. For viewmodel we can create a folder name viewmodel, then create a custom class and keep that property which we need. I tried to clear myself. Any suggestion highly appreciated.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionautonomattView Question on Stackoverflow
Solution 1 - asp.net MvcDaniel AugerView Answer on Stackoverflow
Solution 2 - asp.net MvcRayView Answer on Stackoverflow
Solution 3 - asp.net Mvcstiank81View Answer on Stackoverflow
Solution 4 - asp.net MvcDavidView Answer on Stackoverflow
Solution 5 - asp.net Mvcriadh gomriView Answer on Stackoverflow
Solution 6 - asp.net MvcsgwillView Answer on Stackoverflow
Solution 7 - asp.net MvcAbolfazlRView Answer on Stackoverflow
Solution 8 - asp.net MvcLalit KhannaView Answer on Stackoverflow
Solution 9 - asp.net MvcMd. Saddam HossainView Answer on Stackoverflow