views:

413

answers:

4

I read this article today http://dotnetslackers.com/articles/silverlight/Silverlight-3-and-the-Data-Form-Control-part-I.aspx about the use of the MVVM pattern within a silverlight app where you have your domain entities and view spesific entities which basically is a subset of the real entity objects. Isn't this a clear violation of the DRY principle? and if so how can you deal with it in a nice way?

+1  A: 

DRY is a principle, not a hard rule. You are a human and can differentiate. E.g. If DRY really was a hard rule you would never assign the same value to two different variables. I guess in any non trivial program you would have more than one variable containing the value 0.

Generally speaking: DRY does usually not apply to data. Those view specific entities would probably only be data transfer objects without any noteworthy logic. Data may be duplicated for all kinds of reasons.

EricSchaefer
Still I find this pretty bad as you would create light entity classes for the viewmodel and repeat the for the actual entities
TT
Why do you find this bad?
EricSchaefer
I think its a bad violation of dry because you are creating 2 almost identical classes representing the same entity also without using inheritance
TT
I don't know about Silverlight, but sometimes this may be reasonable, if you want to decouple systems.
EricSchaefer
+6  A: 

Personally, I don't like what Dino's doing there and I wouldn't approach the problem the same way. I usually think of a VM as a filtered, grouped and sorted collections of Model classes. A VM to me is a direct mapping to the View, so I might create a NewOrderViewModel class that has multiple CollectionViews used by the View (maybe one CV for Customers and another CV for Products, probably both filtered). Creating an entirely new VM class for every class in the Model does violate DRY in my opinion. I would rather use derivation or partial classes to augment the Model where necessary, adding in View specific (often calculated) properties. IMO .NET RIA Services is an excellent implementation of combining M and VM data with the added bonus that it's usable in on both the client and the server. Dino's a brilliant guy, but way to call him out on this one.

James Cadd
Agreed, The VM should just be those parts of the model that the View should know about.
Graeme Bradbury
+1  A: 

I think the answer really depends on what you feel should be in the ViewModel. For me the ViewModel represents the model of the screen currently being displayed.

So for something like a ViewCategoryViewModel, I don't have a duplication of the fields in Category. I expose a Category object as a property on the ViewModel (under say "SelectedCategory"), any other data the view needs to display and the Commands that screen can take.

There will always be some similarity between the domain model and the view model, but it all comes down to how you choose to create the ViewModel.

Nigel Sampson
A: 

It's the same as with Data Transfer Objects (DTO).

The domain for those two object types is different, so it's not a violation of DRY.

Consider the following example:

class Customer
{
    public int Age
}

And a corsponding view model:

class CustomerViewModel
{
    public string Age;

    // WPF validation code is going to be a bit more complicated:
    public bool IsValid() 
    {
        return string.IsNullOrEmpty(Age) == false;
    }
}

Differnt domains - differnet property types - different objects.

Pawel Lesnikowski
1. What is a domain in this context? 2. Your business model have a customer entity. Imagine you have a Customer entity, a customer DTO and a CustomerViewModel, you would of course have a lot of duplicate properties there. I see this as a clear violation of DRY
TT
1. Customer is probably mapped with NH to the database, it may even contain some hacks because of not-so-perfect db schema. It has many business methods, collections, which are most likely not needed when you display it on the screen. 2. Single Responsibility Principle (SRP) says that object should have only one reason to change, if Customer class is used as business model, DTO, and ViewModel in MVVM, than it clearly volatiles SRP. Note that I agree that there are situations when it's easier to have just a single class, it all depends on the context.
Pawel Lesnikowski
Consider also properties like IsSelected on ViewModel and canceling the edition on screen (you need to revert Customer object, if you are not binding some CustomerViewModel class), implementation on of INotifyPropertyChanged. Only in a very simplistic scenario it seems like violation of DRY.
Pawel Lesnikowski