views:

260

answers:

1

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)

Blockquote 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!

+4  A: 

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());
R0MANARMY
That helps. Would this work on source properties as well? I want to be able to add new properties to source, and only source, and not worry about breaking the destination (viewmodel).
Echiban
Any property that's on the source, but not on the destination will be ignored automatically.
R0MANARMY
Thank you. This really clears things up.
Echiban