AutoMapper嵌套映射

伊曼纽尔·伊斯塔斯(Emmanuel Istace)

所以我有一个用于DI的接口:

public interface IMapper<in TIn, out TOut>
{
    TOut Map(TIn objectToMap);
}

和我的映射器的基类:

public abstract class MapperBase<TIn, TOut> : IMapper<TIn, TOut>
{
    internal IConfigurationProvider MapperConfiguration { get; set; }

    public TOut Map(TIn objectToMap)
    {
        return MapperConfiguration.CreateMapper().Map<TOut>(objectToMap);
    }
}

我将使用以下简化的数据模型(DAL和Service层“相同”)

public class Client
{
    public int Id { get; set; }
    public List<Contract> Contracts { get; set; }
}

public class Contract
{
    public int Id { get; set; }
    public List<Installation> Installations { get; set; }
}

public class Installation
{
    public int Id { get; set; }
}

我的DAL和Service层之间有以下映射器:

public class DalClientToServiceMapper : MapperBase<DAL.Client, Interface.Model.Client>
{
    public DalClientToServiceMapper(IMapper<DAL.Contract, Interface.Model.Contract> contractMapper)
    {
        MapperConfiguration = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<DAL.Client, Interface.Model.Client>();
            cfg.CreateMap<DAL.Contract, Interface.Model.Contract>().ConstructUsing(contractMapper.Map);
        });
    }
}

public class DalContractToServiceMapper : MapperBase<DAL.Contract, Interface.Model.Contract>
{
    public DalContractToServiceMapper(IMapper<DAL.Installation, Interface.Model.Installation> dalInstallationToServiceMapper)
    {
        MapperConfiguration = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<DAL.Contract, Interface.Model.Contract>();
            cfg.CreateMap<DAL.Installation, Interface.Model.Installation>().ConstructUsing(dalInstallationToServiceMapper.Map);
        });
    }
}

public class DalInstallationToServiceMapper : MapperBase<DAL.Installation, Interface.Model.Installation>
{
    public DalInstallationToServiceMapper()
    {
        MapperConfiguration = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<DAL.Installation, Interface.Model.Installation>();
        });
    }
}

但是,当我映射一个客户端时,AutoMapper给了我一个例外,说没有为安装定义任何映射(嵌套嵌套类型)。

当我在客户端映射器中为安装配置映射时,它可以正常工作。

我不明白的是,我提供了一种特定的方法来映射“客户端”中的嵌套类型“合同”,以及一种特定的方法来映射“合同”中的嵌套“安装”,为什么自动映射器试图将其映射配置用于嵌套类型?他为什么不只是停在那里并使用我提供的方法?如何实现我在这里想要做的,将映射器链接起来?

伊曼纽尔·伊斯塔斯(Emmanuel Istace)

我找到了使用配置文件的解决方案。

我在从概要文件派生的类中定义了所有映射:

public class DalToServiceProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<DAL.Client, Interface.Model.Client>();
        CreateMap<DAL.Installation, Interface.Model.Installation>();
        CreateMap<DAL.Contract, Interface.Model.Contract>();
    }
}

我将基类更改为如下形式:

public abstract class MapperBase<TIn, TOut, TProfile> : IMapper<TIn, TOut> where TProfile : Profile, new()
{
    internal IConfigurationProvider MapperConfiguration { get; set; }

    protected MapperBase()
    {
        MapperConfiguration = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile<TProfile>();
        });
    }

    public TOut Map(TIn objectToMap)
    {
        return MapperConfiguration.CreateMapper().Map<TOut>(objectToMap);
    }
}

现在,我的Mappers ar更简单了:

public class DalClientToServiceMapper : MapperBase<DAL.Client, Interface.Model.Client, DalToServiceProfile>
{
}

public class DalContractToServiceMapper : MapperBase<DAL.Contract, Interface.Model.Contract, DalToServiceProfile>
{
}

public class DalInstallationToServiceMapper : MapperBase<DAL.Installation, Interface.Model.Installation, DalToServiceProfile>
{
}

我需要这个额外的层,因为我们正在使用依赖注入,并且需要根据其接口注入映射器。

我所有的映射器都知道所有映射配置,但是在我的情况下,我认为这没什么大不了的。

同样,它们没有链接,但是由于它们每个都只加载相同的概要文件,因此它们执行相同的操作。

实际上,我在这里所做的只是通过使用IMapper接口将映射器解耦,因此我可以轻松注入映射器并使用强类型化的映射器。Cleaner然后在我的服务Mapper.Map中的任何地方调用或在我的服务外观中创建mapper。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Automapper嵌套映射以及投影

AutoMapper 9映射嵌套表

Automapper 嵌套关系“返回”映射

Automapper:如何映射嵌套对象?

Automapper Aftermap 替代嵌套映射

Automapper映射嵌套了不同的对象属性

映射类型 AutoMapper 中嵌套对象的问题

使用 entityFramework Automapper .Net Core 嵌套映射

Automapper 链/嵌套对象映射抛出异常

AutoMapper 使用数组列表的嵌套属性映射

AutoMapper和手动映射嵌套的复杂类型

Automapper 非常简单的嵌套映射不起作用

使用AutoMapper将嵌套元素映射到相关列表

AutoMapper:为没有映射的嵌套属性设置参考

Automapper通过分组将表映射到嵌套类

映射两个相同的模型,嵌套有 automapper

C# automapper 嵌套對象條件映射

AutoMapper - 将源对象映射到嵌套对象

如何在不使用内联映射或多个.ForMember的情况下在automapper中映射嵌套对象?

AutoMapper自定义类型转换器ITypeConverter和映射嵌套对象

使用Automapper从嵌套了多个级别的列表中映射/转换为父属性

AutoMapper:映射到由接口定义的嵌套对象中包含的私有 setter

如何使用AutoMapper将嵌套列表映射到另一个列表

Automapper-将简单列表映射到复杂的嵌套类型

Automapper 配置文件将具有嵌套列表的对象映射到目标列表

如何使用 Automapper 或 LINQ 将具有嵌套列表的对象映射到对象列表?

AutoMapper通用映射

AutoMapper映射对象类型

AutoMapper的映射问题?