Reputation: 847
I am building a MVVM application. The model / entity (I am using NHibernate) is already done, and I am thinking of using AutoMapper to map between the ViewModel and Model.
However this clause scares the jebus out of me: (from http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/01/22/automapper-the-object-object-mapper.aspx)
AutoMapper enforces that for each type map (source/destination pair), all of the properties on the destination type are matched up with something on the source type
To me, the logical choice is to map from model to viewmodel, (and I'll let viewmodel manually assign to model), but the quote basically kills the idea since the viewmodel will definitely have properties that don't exist on the model.
How have you been using Automapper in a MVVM app? Please help!
Upvotes: 3
Views: 4446
Reputation: 56
I was wondering if someone has tried to do smth like this:
public bool SetMappedProperty<TC,TV>(ref TC cont, TV value, [CallerMemberName] string propertyName = null)
{
var prop = cont.GetType().GetProperty(propertyName);
var old = prop.GetValue(cont, null);
if (Equals(old, value)) { return false; }
prop.SetValue(cont, value);
RaisePropertyChanged(propertyName);
return true;
}
and used it like this:
public override MyType MyProperty
{
get { return _myData.MyProperty; }
set { SetMappedProperty( ref _myData, value); }
}
reactive extensions WhenanyValue -things could also help.
Upvotes: 0
Reputation: 430
You could investigate using Polymod. Polymod proxies are essentially view models that wrap your nhibernate objects. With it's formula feature you can add self-updating properties such as IsComboVisible = (domainobject.A + domainobject.B > 10)
Upvotes: 0
Reputation: 20246
When it says "map" it doesn't mean it's a 1 to 1 mapping, it just means all of your properties need to be accounted for. Either Automapper can figure it out from convention, you explicitly map them, or explicitly tell it to ignore a given property.
Here's the example from the documentation. As you can see, the property is mapped in a sense that it's accounted for, but Automapper knows to just ignore it.
Mapper.CreateMap<Source, Destination>()
.ForMember(dest => dest.SomeValuefff, opt => opt.Ignore());
Upvotes: 7