Reputation: 1259
I have been reading about the difference between DTO's and Viewmodels, in which the main one is the presence of behaviors in the Viewmodels compared to DTOs which just carry data through layers, but is not clearly stated the definition for behaviors as far as I read.
Viewmodels can transform the data passed in properties from a DTO to be formatted in a certain way for a certain view and also in WPF events can be added to VM's which may enrich their purpose. So what exactly "behavior" means on this case? Thanks.
Upvotes: 3
Views: 742
Reputation: 12142
As you will have read a DTO
is a "Data Transfer Object".
Loosely speaking Data
is one aspect of an object, the other being Behavior'. 'Behavior
is again loosely speaking simply a set of related actions(methods) and events that acts upon that Data
.
So an object is a container for some data and a bunch of behaviors that are related to that data. DTOs
and ViewModels
are both objects but they have different purposes in life and as a result have a different emphasis on the behavior they contain.
A DTO
is 'only' concerned with Transporting
data between, for instance, process boundaries, application tiers or networks.
As a result DTOs
often have little if any behavior because behavior is actions and actions are only useful when we want to do something with the data.
Since Transportation / serialization has a transport cost we usually just want these DTO
s, to be lean and mean.
However once the DTO
lands at it's destination, we'll typically want to do something with the data it contains. So we peel the data out of the DTO
, thanks very much for a job well done in getting it here, and stuff it into a ViewModel
so we can interact with the data in some way via behaviours (methods and events) typically via some UI.
So behaviors are things like formatting a value on edit / save, triggering a change in propertyB based on the updated value in propertyA, etc.
INotifyingPropertyChanged
(INPC) interface will often be implemented on a ViewModel
to help with this.
So since behavior is simply options for interacting with the data ( properties, methods, events), ViewModels
have this because that's what we build views for, interacting with data. DTOs' on the other hand often / typically don't have (much) behavior because their function is transport not interaction.
Upvotes: 6
Reputation: 10286
Generally a DTO is what its name suggests, a simple object without any behaviour that is used to transfer data across system boundaries. However Viewmodel purpose is seperation of concern.It is the responsibilty of view model to decouple you view from model.It can perform many operations like toggle view elements.validation run some custom logic which DTO cannot.That is your viewmodel behavior.
Upvotes: 2