Automapper-如何从源子对象映射到目标 [英] Automapper - How to map from source child object to destination

查看:119
本文介绍了Automapper-如何从源子对象映射到目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从源的子对象映射到目标(作为父对象).

I am trying to map from a child object of source to destination(as parent object).

源模型:

public class SourceBaseResponse<T> where T : new()
{
    public string Type { get; set; }

    public string Id { get; set; }

    public T Attributes { get; set; }
}

在我的示例中,我使用T为SourceAssignment类型

For my example I am using T to be of type SourceAssignment

 public class SourceAssignment
{
    public string Id { get; set; }

    public string Email { get; set; }

    public string EmployeeId { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public DateTimeOffset CreatedAt { get; set; }

}

目标对象

public class DestinationAssignment
{
    public string Id { get; set; }

    public string Email { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }
}

我想直接将源模型映射到目标.所以,我试图使用

I want to map Source Model directly to Destination. So, I was trying to use

CreateMap<SourceAssignment, DestinationAssignment>();
CreateMap<SourceBaseResponse<SourceAssignment>, DestinationAssignment>()
            .ForMember(dest => dest, opt => opt.MapFrom(src => AutoMapperConfig.Mapper.Map<DestinationAssignment>(src.Attributes)));

这不起作用,因为我在上一行中遇到运行时错误,即类型的顶级单个成员仅支持成员的自定义配置."

This is not working as I am getting run time error in the above line that "Custom configuration for members is only supported for top-level individual members on a type."

因此,根据

So, as per this thread I tried the following

CreateMap<SourceBaseResponse<SourceAssignment>, DestinationAssignment>()
            .AfterMap((src, dst) => Mapper.Map(src.Attributes, dst));

现在,我在应该进行映射的地方遇到错误,提示未初始化映射器.以适当的配置调用初始化.如果您试图通过容器或其他方式使用映射器实例,请确保您没有对容器的任何调用.静态Mapper.Map方法,如果使用的是ProjectTo或UseAsDataSource扩展方法,请确保传递适当的IConfigurationProvider实例."

Now, I am getting error where mapping should happen which says "Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance."

我能够为每个属性使用ForMember并将其从src.Attributes映射到dest(例如:src.Attribute.Id到dest.Id).这行得通,但是我真的不想这样做,因为我的Source是涉及嵌套子级的复杂类(因为这是Web API响应,因此我无法控制).因此,在这里完成了许多自定义映射

I am able to use ForMember for each property and map it from src.Attributes to dest(For eg: src.Attribute.Id to dest.Id). This works, but I do not really want to do this as my Source are complex classes involving nested childs(as this is a Web API response and I do not have control over this). So a lot of custom mapping is done here

CreateMap<SourceAssignment, DestinationAssignment>();

有关如何进行的任何建议.

Any suggestions on how to proceed.

推荐答案

需要能够使用解析上下文才能调用Mapper.Map(),您可以使用ConstructUsing()获取解析上下文:

Resolution context is needed to be able to call Mapper.Map(), you can get resolution context by using ConstructUsing():

CreateMap<SourceChild, Destination>();
CreateMap<Source, Destination>()
    .ConstructUsing((src, ctx) => ctx.Mapper.Map<Destination>(src.SourceChild));

这篇关于Automapper-如何从源子对象映射到目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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