简体   繁体   English

AutoMapper配置文件:展平dto以进行映射

[英]AutoMapper Profiles: flattening a dto for mapping

I have a Main class that has a nested class. 我有一个嵌套类的Main类。 I have used this successfully to map using Mapper class 我已经成功地使用它来使用Mapper类进行映射

public class Main
{
    public string Name { get; set; }
    public List<QuantityLocation> NC { get; set; }
}

public class NestedClass
{
    public decimal B { get; set; }
    public string A { get; set; }
}

public class Flattened
{
    public string Name { get; set; }

    public string A { get; set; }
    public decimal B { get; set; }
}

Mapping done using the Mapper class as below. 使用Mapper类完成映射,如下所示。

    Mapper.CreateMap<NestedClass, Flattened>();
    Mapper.CreateMap<Main, Flattened>();
    Mapper.CreateMap<Main, List<Flattened>>()
        .ConvertUsing(i =>
                i.NC.Select(
                    flat =>
                    {
                        var flatList = Mapper.Map<Flattened>(i);
                        Mapper.Map(flat, flatList);
                        return flatList;
                    }).ToList());

Now when I moved this mapping to my Profile class, I had to change the lines above to this below: 现在,当我将此映射移到我的Profile类时,我不得不将上面的行更改为下面的行:

CreateMap<NestedClass, Flattened>();
CreateMap<Main, Flattened>();
CreateMap<Main, List<Flattened>>()
    .ConvertUsing(i =>
            i.NC.Select(
                flat =>
                {
                    var flatList = Mapper.Map<Flattened>(i);
                    Mapper.Map(flat, flatList);
                    return flatList;
                }).ToList());

The problem I am facing is how to convert these two lines below in the snippet above. 我面临的问题是如何在上面的代码片段中转换这两行。

               var flatList = Mapper.Map<Flattened>(i);
                Mapper.Map(flat, flatList);

you see I am injecting the Mapper.Engine to the constructor of my controllers. 您会看到我正在将Mapper.Engine注入控制器的构造函数。 Earlier I was just using the Static Mapper class which used to get called in my global.asax. 之前我只是使用Static Mapper类,该类曾经在global.asax中被调用。 Now I get an error like the one below. 现在我收到类似下面的错误。

Missing type map configuration or unsupported mapping.

Mapping types: Main -> List`1 MyProj.Main -> System.Collections.Generic.List`1[[MyProj.Flattened, MyProj, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

Destination path: List`1

Source value: MyProj.Main

The proper way to do is to use a custom type converter , in which you inject the mapping engine itself. 正确的方法是使用自定义类型转换器 ,在其中注入映射引擎本身。 And use ConstructServicesUsing in your profile declaration. 并在您的配置文件声明中使用ConstructServicesUsing

Assuming you use some of IoC container, and you have registered in it the mapping engine, and the custom type converter, inside the converter you will use engine.Map instead the static Mapper. 假设您使用了一些IoC容器,并且已经在其中注册了映射引擎和自定义类型转换器,则在转换器内部,您将使用engine.Map而不是静态Mapper。

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

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