Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动映射基类映射_C#_Automapper - Fatal编程技术网

C# 自动映射基类映射

C# 自动映射基类映射,c#,automapper,C#,Automapper,对于AutoMapper,我已经实现了以下功能,但有重复的映射代码: cfg.CreateMap<RmsOelEntryUnlinkedPersonInputDto, AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedPersonUnlinked>() .ForMember(dest => dest.DateOfBirth, opts => opts.MapFrom(src => FormatDateType(src

对于AutoMapper,我已经实现了以下功能,但有重复的映射代码:

cfg.CreateMap<RmsOelEntryUnlinkedPersonInputDto, AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedPersonUnlinked>()
    .ForMember(dest => dest.DateOfBirth, opts => opts.MapFrom(src => FormatDateType(src.DateOfBirth)))
    .ForMember(dest => dest.EffectiveFromTime, opts => opts.MapFrom(src => FormatDateTimeType(src.EffectiveFromTime)))
    .ForMember(dest => dest.EffectiveToTime, opts => opts.MapFrom(src => FormatDateTimeType(src.EffectiveToTime)));

cfg.CreateMap<RmsOelEntryUnlinkedAddressInputDto, AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedAddressUnlinked>()
    .ForMember(dest => dest.EffectiveFromTime, opts => opts.MapFrom(src => FormatDateTimeType(src.EffectiveFromTime)))
    .ForMember(dest => dest.EffectiveToTime, opts => opts.MapFrom(src => FormatDateTimeType(src.EffectiveToTime)));
cfg.CreateMap()
.FormMember(dest=>dest.DateOfBirth,opts=>opts.MapFrom(src=>FormatDateType(src.DateOfBirth)))
.FormMember(dest=>dest.EffectiveFromTime,opts=>opts.MapFrom(src=>FormatDateTimeType(src.EffectiveFromTime)))
.FormMember(dest=>dest.EffectiveToTime,opts=>opts.MapFrom(src=>FormatDateTimeType(src.EffectiveToTime));
cfg.CreateMap()
.FormMember(dest=>dest.EffectiveFromTime,opts=>opts.MapFrom(src=>FormatDateTimeType(src.EffectiveFromTime)))
.FormMember(dest=>dest.EffectiveToTime,opts=>opts.MapFrom(src=>FormatDateTimeType(src.EffectiveToTime));
RMSOELENTRYUNLinkedPersonInputTo
RMSOELENTRYUNLinkedAddressInputTo
都是从
RmsOelEntryInvolvedEntityBaseDto
继承而来的,这个基类具有属性
EffectiveFromTime
EffectiveToTime

我不希望必须重复映射
EffectiveFromTime
EffectiveToTime
,如上所示

但是,
addNewoElentRyinVolvedEntities UnlinkedinVolvedPersonUnlinked
addNewoElentRyinVolvedEntities UnlinkedinVolvedAddressUnlinked
是自动生成的,没有基类。因此,我不知道如何使用AutoMapper的“包含”映射选项


我如何优化它以消除重复映射?

我也遇到过类似的情况,并求助于助手扩展方法。我为您的案例定制了:

internal static class CommonMapperExtensions
{
     internal static IMappingExpression<TSource, TDestination> MapCommonFields<TSource, TDestination>(this IMappingExpression<TSource, TDestination> m)
         where TSource : RmsOelEntryInvolvedEntityBaseDto
         where TDestination : IEffective
     {
         return m
            .ForMember(dest => dest.EffectiveFromTime, opts => opts.MapFrom(src => FormatDateTimeType(src.EffectiveFromTime)))
            .ForMember(dest => dest.EffectiveToTime, opts => opts.MapFrom(src => FormatDateTimeType(src.EffectiveToTime)));                
     }
}
公共类TBaseModel
{
public void ConfigureMapFrom(IMappingExpression映射)
{
//…映射
}
}
公共类TModel:TBaseModel
{
public void ConfigureMapFrom(IMappingExpression映射)
{
mapping.IncludeBase();
//…其他映射
}
}

谢谢。在您的扩展方法中,我看到目标被约束为AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedPersonUnlinked,但它还需要映射AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedAddressUnlinked。如果生成的类是部分的,那么这将是一个开放的问题。当然,如果这种情况比两个映射更常见,那么这样做会有回报。它们确实是部分的,并且您的解决方案工作得很好;尽管我需要修改代码以引用扩展方法中的“m”对象。非常感谢。太好了,我也修复了答案中的错误。你们能为你们的答案提供更多的上下文吗?
cfg.CreateMap<RmsOelEntryUnlinkedPersonInputDto, AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedPersonUnlinked>()
    .MapCommonFields()
    .ForMember(dest => dest.DateOfBirth, opts => opts.MapFrom(src => FormatDateType(src.DateOfBirth)));

cfg.CreateMap<RmsOelEntryUnlinkedAddressInputDto, AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedAddressUnlinked>()
    .MapCommonFields();
public interface IEffective 
{
     DateTime EffectiveFromTime {get; set;}
     DateTime EffectiveToTime {get; set;}
}

public partial AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedAddressUnlinked
    : IEffective { }

public partial AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedPersonUnlinked
    : IEffective { }
public class TBaseModel
{
    public void ConfigureMapFrom(IMappingExpression<TEntity, TBaseModel> mapping)
    {
        // ... mappings
    }
}

public class TModel : TBaseModel
{
    public void ConfigureMapFrom(IMappingExpression<TEntity, TModel> mapping)
    {
        mapping.IncludeBase<TEntity, TBaseModel>();

        // ... other mappings
    }
}