简体   繁体   English

自动映射器公约

[英]Automapper Convention

Is is possible with Automapper to setup a convention so that maps do not have to be created by hand for situations where the entity you are mapping to just has say "ViewModel" appended. Automapper可以设置一个约定,这样在要映射到的实体仅附加了“ ViewModel”的情况下,不必手动创建映射。

As an example I would rather not have to setup the following map: 作为示例,我宁愿不必设置以下地图:

Mapper.CreateMap<Error, ErrorViewModel>();

I understand if projection is required that I would need to create a custom map, but having a convention to create maps would be nice. 我了解如果需要投影,则需要创建自定义地图,但是约定创建地图会很好。

You would need to use Mapper.DynamicMap<TDest>(source) to map. 您将需要使用Mapper.DynamicMap<TDest>(source)进行映射。

As you can see in the example below, it automatically maps the matching 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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM