How have you combined the benefits of the direct View-to-Model and MVVM approach in your WPF projects?

There are many Model objects in our application that have hundreds of properties .

For each property in the model :

public string SubscriptionKind { get; set; }
...100x...

      

we needed to include a property with INotifyPropertyChanged support in the ViewModel :

#region ViewModelProperty: SubscriptionKind
private int _subscriptionKind;
public int SubscriptionKind
{
    get
    {
        return _subscriptionKind;
    }

    set
    {
        _subscriptionKind = value;
        OnPropertyChanged("SubscriptionKind");
    }
}
#endregion

...100x...

      

which meant that when our View dispatched a Save event , we had to reassign all of these viewmodel values ​​back to the model:

customer.SubscriptionKind = this.SubscriptionKind
...100x...

      

This became tedious and time-consuming as the models changed and we had to map all changes in the ViewModels.

After a while, we realized that the easiest way is to directly bind the DataContext in the view to the model, which will allow us to directly bind the XAML elements to the properties of the Model object so that the save event will simply save the object without any mapping.

What we lost in this turn:

  • the ability through UpdateSourceTrigger=PropertyChanged

    small-scale validation and manipulation of the ViewModel property networks, which I really liked: we don't have that anymore, since any change in XAML just changes the dumb property on the model

  • the ability (in the future) to create mock views that test our viewmodel UI logic in a new way, e.g. "if the SubscriptionKind property is set to" Yearly "then (1) change the discount to 10%, (2) run the" congratulations animation "and (3) make the order button more visible.

Both of these approaches have obvious advantages , eg. the first "View-direct-to-Model" approach, especially when combined with LINQ-to-SQL , is pragmatic and allows you to quickly create useful software, and as long as you use it {Binding...}

instead x:Name

you still have the option to "convey your views to the Blend designer ".

On the other hand, while MVVM requires you to maintain tedious model-to-ViewModel mapping, it gives you powerful validation and testing that the first approach doesn't.

How have you been able to combine the benefits of these two approaches in your projects?

+2


source to share


4 answers


Since your ViewModel has access to the model, you can just simply transfer the properties of the model:

#region ViewModelProperty: SubscriptionKindprivate
// int _subscriptionKind; - Use the model directly
public int SubscriptionKind
{
    get
    {
        return this.Model.SubscriptionKind;
    }
    set
    {
        if (this.Model.SubscriptionKind != value)
        {
            this.Model.SubscriptionKind = value;
            OnPropertyChanged("SubscriptionKind");
        }
    }
}
#endregion

      



The advantage here is that you can keep your validation in place in the ViewModel if you want, and have more control over how it is returned to your model, but there is less duplication.

+1


source


Why not use a mapping tool like AutoMapper ? It's fast and you don't have to write all the display code:

Mapper.CreateMap<MyModel, MyViewModel>();
MyViewModel vm = Mapper.Map(myModelInstance);

      



Really easy and now you get the best of both worlds.

Automapper uses a technique that generates assemblies on the fly to perform the mapping. This makes it execute as quickly as if you had written all that tedious mapping code, but you don't have to.

+1


source


Since my model objects are business objects not directly related to the datamodel, I use them directly in the ViewModel.

The first mapping (datamodel to business object model) and property creation are generated by the code generator.

0


source


I used the t4 generator class to create my ViewModels from XAML, not sure if this will help your situation.

0


source







All Articles