所以我有一个用于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给了我一个例外,说没有为安装定义任何映射(嵌套嵌套类型)。
当我在客户端映射器中为安装配置映射时,它可以正常工作。
我不明白的是,我提供了一种特定的方法来映射“客户端”中的嵌套类型“合同”,以及一种特定的方法来映射“合同”中的嵌套“安装”,为什么自动映射器试图将其映射配置用于嵌套类型?他为什么不只是停在那里并使用我提供的方法?如何实现我在这里想要做的,将映射器链接起来?
我找到了使用配置文件的解决方案。
我在从概要文件派生的类中定义了所有映射:
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] 删除。
我来说两句