Using Automapper in an MVVM Application

I am creating an MVVM application. The model / entity (I use NHibernate) has already been executed, and I'm thinking of using AutoMapper to map between ViewModel and Model.

However, this article scares me from jebus: (from http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/01/22/automapper-the-object-object-mapper.aspx )

AutoMapper ensures that for each type map (source / destination pair) all properties of the destination type are mapped to something of the source type

For me, the logical choice is to map the model to the viewmodel (and I will let the viewmodel manually assign the models), but the quote basically kills this idea, since the viewmodel will definitely have properties that are not in the model.

How do you use Automapper in an MVVM application? Please, help!

+3
source share
3 answers

When he says "map", this does not mean that this mapping is from 1 to 1, it just means that all your properties should be taken into account. Any Automapper can understand this from the convention; you explicitly match them or explicitly instruct them to ignore this property.

Here is an example from the documentation. As you can see, the property is displayed in the sense in which it is taken into account, but Automapper knows that it simply ignores it.

Mapper.CreateMap<Source, Destination>()
    .ForMember(dest => dest.SomeValuefff, opt => opt.Ignore());
+7
source

Polymod. - Polymod - , , , nhibernate. , IsComboVisible = (domainobject.A + domainobject.B > 10)

0

I was wondering if anyone was trying to do something 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); }
    }

WhenanyValue-things reactive extensions can also help.

0
source

Source: https://habr.com/ru/post/1740187/


All Articles