如何使用Automapper映射到具有多个实现的类 [英] How to map to class with multiple implementations using Automapper

查看:180
本文介绍了如何使用Automapper映射到具有多个实现的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JSON格式的配置文件,其中包含我的课程的配置列表.我将配置读取为具有所有常见配置的BaseDTO类,并且我想根据字段Type将那些BaseDTO映射到Base类的特定实现.有什么方法可以告诉automapper使用一些自定义映射逻辑来告诉它要创建哪个类?

I have configuration file in JSON format that have a list of configurations for my classes. 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. Is there any way to tell automapper to use some custom mapping logic to tell it which class to create?

    //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
    {

    }

推荐答案

您可以使用 ConstructUsing 方法根据 Type 属性定义要创建的对象BaseDTO 类,然后在 BeforeMap 中将源 BaseDTO 实例映射到创建的目标对象.例如,我允许我对您的课程进行一些更改

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. 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>();
    });

Create 方法的主体是(您可以创建一个单独的工厂类或它)

Body of Create method is (you can create a separate factory class or it)

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");
    }
}

下面的示例演示了它的工作方式(第一个对象映射到 A 类,第二个对象映射到 B ,...)

And following example shows how it works (first object is mapped to A class, second to 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);

这篇关于如何使用Automapper映射到具有多个实现的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆