【问题标题】:AutoMapper Nested MappingsAutoMapper 嵌套映射
【发布时间】:2017-01-26 09:04:08
【问题描述】:

所以我有一个接口,用于 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 和服务层“相同”)

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 和服务层之间有以下映射器:

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 给了我一个异常,说没有为安装定义映射(嵌套嵌套类型)。

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

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

【问题讨论】:

    标签: c# .net automapper


    【解决方案1】:

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

    我在派生自配置文件的类中定义所有映射:

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

    我的映射器现在更简单了:

    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 中的任何地方调用或在我的服务外观中创建映射器。

    【讨论】:

      猜你喜欢
      • 2020-07-17
      • 2017-07-20
      • 2020-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多