Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper use in a MVVM application

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!

like image 303
Echiban Avatar asked Feb 03 '26 06:02

Echiban


1 Answers

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());
like image 172
Roman Avatar answered Feb 05 '26 07:02

Roman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!