Introduction
I've been developing a tool to help the other devs on the team deal with the creation of menu items for their MVC apps. The tool in question is a WPF desktop app that utilizes the MVVM pattern. I was writing the models and associated view models when it struck me that I'd been doing it "the hard way" all these years. I was writing a series of methods *in every viewmodel* that shuttled data back and forth between the model and the view model. For some of the models, this involved 20 or more properties, and the coding required to support them was pure drudgery. Yesterday, I came up with an alternative.
The Code
I created a class with the following code, and inherited that class from my view model classes:
public enum MVVMDirection { FROM, TO };
public class VMBase
{
public void UpdateFromModel<TModel>(TModel model)
{
this.Update<TModel>(model, MVVMDirection.FROM);
}
public void UpdateToModel<TModel>(TModel model)
{
this.Update<TModel>(model, MVVMDirection.TO);
}
public void Update<TModel>(TModel model, MVVMDirection direction)
{
PropertyInfo[] mProperties = model.GetType().GetProperties();
PropertyInfo[] vmProperties = this.GetType().GetProperties();
foreach(PropertyInfo mProperty in mProperties)
{
PropertyInfo vmProperty = this.GetType().GetProperty(mProperty.Name);
if (vmProperty != null)
{
if (vmProperty.PropertyType.Equals(mProperty.PropertyType))
{
if (direction == MVVMDirection.FROM)
{
vmProperty.SetValue(this, mProperty.GetValue(model));
}
else
{
vmProperty.SetValue(model, mProperty.GetValue(this));
}
}
}
}
}
}
Example Usage
As with any code, there is more than one way to use these methods. In my own case, my typical ViewModel
object contains a property that represents the model, and the constructor takes a model object parameter. After setting the Model
property from the parameter, the constructor performs an initial call to the UpdateFromModel()
method. My view models look something like this:
public partial class MyViewModel : ViewModelBase
{
private string property1;
public MyModel Model { get; set; }
public string Property1
{
get { return this.property1; }
set
{
if (this.property1 != value)
{
this.property1 = value;
this.NotifyPropertyChanged();
}
}
}
public MyViewModel(MyModel model)
{
this.Model = model;
this.UpdateFromModel<mymodel>(this.Model);
}
}
I use a Model
property because I don't want to commit changes to the model's data until the user is ready to do so (determined by actions in the UI). Of course, you could directly update the model's value from the viewmodel property (via app UI actions) if that's what you need to do, but that's not the way *I* do it. As for the data transfer between object properties, you can likely imagine a long list of this kind of code, you can probably understand why I came up with these base class methods.
I understand that some of you reading this may have a different outlook on life as it relates to where you put code and how you implement the MVVM pattern, so here's an example of doing it a little different, you can also perform that initial method call externally of the viewmodel
object after instantiating the view model, like so:
MyModel modelObj = new MyModel();
VMMyModel vmObj = new VMMyModel();
vmObj.UpdateFromModel<MyModel>(modelObj);
vmObj.UpdateToModel<MyModel>(modelObj);
modelObj.CallYourSaveMethod();
I can't possibly conjure up a definitive list of use cases, and I'm honestly not interested in religious discussions regarding implementation styles, so I hope you get the idea of how to utilize the code. Go forth, and mould it into your own image.
Change History
- 2019.08.03 - Fixed the
Update
method to remove references to the viewmodel
parameter