C# 自动映射嵌套映射

C# 自动映射嵌套映射,c#,.net,automapper,C#,.net,Automapper,所以我有一个接口,用于DI: public interface IMapper<in TIn, out TOut> { TOut Map(TIn objectToMap); } 公共接口IMapper { TOut地图(TIn对象地图); } 以及我的映射器的基类: public abstract class MapperBase<TIn, TOut> : IMapper<TIn, TOut> { internal IConfigurati

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

public interface IMapper<in TIn, out TOut>
{
    TOut Map(TIn objectToMap);
}
公共接口IMapper
{
TOut地图(TIn对象地图);
}
以及我的映射器的基类:

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);
    }
}
公共抽象类MapperBase:IMapper
{
内部IConfigurationProvider MapperConfiguration{get;set;}
公众兜售地图(TIn对象地图)
{
返回MapperConfiguration.CreateMapper().Map(objectToMap);
}
}
我将使用以下简化的数据模型(“DAL和服务层相同”)

公共类客户端
{
公共int Id{get;set;}
公共列表契约{get;set;}
}
公共类合同
{
公共int Id{get;set;}
公共列表安装{get;set;}
}
公共类安装
{
公共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>();
        });
    }
}
公共类DalClientToServiceMapper:MapperBase
{
公共DALClientToServiceApper(IMapper contractMapper)
{
MapperConfiguration=新的MapperConfiguration(cfg=>
{
CreateMap();
cfg.CreateMap().ConstructUsing(contractMapper.Map);
});
}
}
公共类DalContractToServiceApper:MapperBase
{
公共DALContractToServiceApper(IMapper DAINSTALLATIONTOServiceApper)
{
MapperConfiguration=新的MapperConfiguration(cfg=>
{
CreateMap();
cfg.CreateMap().ConstructUsing(daInstallationToServiceMapper.Map);
});
}
}
公共类DaInstallationToServiceApper:MapperBase
{
公共数据InstallationToServiceApper()
{
MapperConfiguration=新的MapperConfiguration(cfg=>
{
CreateMap();
});
}
}
但是当我映射一个客户机时,AutoMapper会给我一个异常,说没有为安装定义映射(嵌套类型)

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


我不明白的是,我提供了一个从“客户机”映射嵌套类型“合同”的特定方法,以及一个从“合同”映射“安装”的特定方法,那么为什么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>();
    }
}
公共类DalToServiceProfile:配置文件
{
受保护的覆盖无效配置()
{
CreateMap();
CreateMap();
CreateMap();
}
}
我将基类更改为如下所示:

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);
    }
}
公共抽象类MapperBase:IMapper,其中TProfile:Profile,new()
{
内部IConfigurationProvider MapperConfiguration{get;set;}
受保护的MapperBase()
{
MapperConfiguration=新的MapperConfiguration(cfg=>
{
AddProfile();
});
}
公众兜售地图(TIn对象地图)
{
返回MapperConfiguration.CreateMapper().Map(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>
{
}
公共类DalClientToServiceMapper:MapperBase
{
}
公共类DalContractToServiceApper:MapperBase
{
}
公共类DaInstallationToServiceApper:MapperBase
{
}
我需要这个额外的层,因为我们正在使用依赖注入,并且需要根据映射器的接口注入映射器

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

此外,它们没有链接,但由于它们各自只加载相同的配置文件,所以它们执行相同的操作


事实上,我在这里所做的就是通过使用IMapper接口来解耦映射器,这样我就可以轻松地注入映射器并使用强类型映射器。Cleaner然后在我的服务Mapper.Map中调用everywhere或在我的服务facades中创建Mapper。

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

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

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>();
    }
}
公共类DalToServiceProfile:配置文件
{
受保护的覆盖无效配置()
{
CreateMap();
CreateMap();
CreateMap();
}
}
我将基类更改为如下所示:

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);
    }
}
公共抽象类MapperBase:IMapper,其中TProfile:Profile,new()
{
内部IConfigurationProvider MapperConfiguration{get;set;}
受保护的MapperBase()
{
MapperConfiguration=新的MapperConfiguration(cfg=>
{
AddProfile();
});
}
公众兜售地图(TIn对象地图)
{
返回MapperConfiguration.CreateMapper().Map(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>
{
}
公共类DalClientToServiceMapper:MapperBase
{
}
公共类DalContractToServiceApper:MapperBase
{
}
公共类DaInstallationToServiceApper:MapperBase
{
}
我需要这个额外的层,因为我们正在使用依赖注入,并且需要根据映射器的接口注入映射器

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

此外,它们没有链接,但由于它们各自只加载相同的配置文件,所以它们执行相同的操作

事实上,我在这里所做的就是通过使用IMapper接口来解耦映射器,这样我就可以轻松地注入映射器并使用强类型映射器。然后在我的服务Mapper.Map中的任何地方调用或在我的服务facades中创建Mapper