Automapper Convention

You can install Automapper to set up an agreement so that maps are not created manually for situations where the entity to which you are a mapping is simply adding a “ViewModel”.

As an example, I would prefer not to install the following card:

Mapper.CreateMap<Error, ErrorViewModel>();

I understand if a projection is required so that I need to create a custom map, but having an agreement on creating maps would be nice.

+5
source share
1 answer

You will need to use Mapper.DynamicMap<TDest>(source)to display.

As you can see in the example below, it automatically matches the corresponding properties from source to destination.

using AutoMapper;
using System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        var source = new Foo {Value = "Abc"};
        var destination = Mapper.DynamicMap<FooViewModel>(source);

        Debug.Assert(source.Value == destination.Value);
    }
}

public class Foo
{
    public string Value { get; set; }
}

public class FooViewModel
{
    public string Value { get; set; }
}
+6

All Articles