简体   繁体   English

如何使用Automapper映射到具有多个实现的类

[英]How to map to class with multiple implementations using Automapper

I have configuration file in JSON format that have a list of configurations for my classes. 我有JSON格式的配置文件,其中包含我的类的配置列表。 I read configuration to BaseDTO class that have all common configurations and I would like to map those BaseDTO to specific implementations of Base class basing on field Type. 我将配置读取为具有所有常见配置的BaseDTO类,并且我想根据字段Type将那些BaseDTO映射到Base类的特定实现。 Is there any way to tell automapper to use some custom mapping logic to tell it which class to create? 有什么方法可以告诉automapper使用一些自定义映射逻辑来告诉它要创建哪个类?

    //Map in automapper: CreateMap<BaseDTO, Base>();

    public class BaseDTO
    {
        public string Type { get; set; }
        // rest of config from json
    }

    public class Base
    {
        public string Type { get; set; }
        // rest of config mapped from BaseDTO
    }

    public class A : Base
    {

    }

    public class B : Base
    {

    }

    public class C : Base
    {

    }

You can use ConstructUsing method to define which object to create based on Type property of BaseDTO class and then in BeforeMap map source BaseDTO instance to created destination object. 您可以使用ConstructUsing方法基于BaseDTO类的Type属性定义要创建的对象,然后在BeforeMap中将BaseDTO实例映射到已创建的目标对象。 I allowed me to change you classes a little bit for example 例如,我允许我对您的课程进行一些更改

public class BaseDTO
{
    public string Type { get; set; }
    public string AValue { get; set; }
    public string BValue { get; set; }
    public string CValue { get; set; }
}

public class Base
{
    public string Type { get; set; }
}

public class A : Base
{
    public string AValue { get; set; }
}

public class B : Base
{
    public string BValue { get; set; }
}

public class C : Base
{
    public string CValue { get; set; }
}

And I used following automapper configuration 我使用了以下自动映射器配置

var configuration = new MapperConfiguration(
    conf =>
    {
        conf.CreateMap<BaseDTO, Base>()
            .ConstructUsing(s => Create(s.Type))
            .BeforeMap((s, d, c) => c.Mapper.Map(s, d));

        conf.CreateMap<BaseDTO, A>();
        conf.CreateMap<BaseDTO, B>();
        conf.CreateMap<BaseDTO, C>();
    });

Body of Create method is (you can create a separate factory class or it) Create方法的主体是(您可以创建单独的工厂类,也可以创建它)

private static Base Create(string type)
{
    switch (type)
    {
        case "A":
            return new A();
        case "B":
            return new B();
        case "C":
            return new C();
        default:
            throw new Exception("Unknown type");
    }
}

And following example shows how it works (first object is mapped to A class, second to B , ...) 下面的示例演示了它是如何工作的(第一个对象映射到A类,第二个对象映射到B ,...)

var mapper = configuration.CreateMapper();

var dtos = new[]
        {
            new BaseDTO {Type = "A", AValue = "a-value"},
            new BaseDTO {Type = "B", BValue = "b-value"},
            new BaseDTO {Type = "C", CValue = "c-value"}
        };

var result = mapper.Map<Base[]>(dtos);

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

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