简体   繁体   English

自动映射器基类映射

[英]Automapper base class mapping

For AutoMapper, I've implemented the following which works, but has repeated mapping code: 对于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)));

Both RmsOelEntryUnlinkedPersonInputDto and RmsOelEntryUnlinkedAddressInputDto are inherited from RmsOelEntryInvolvedEntityBaseDto and this base class has the properties EffectiveFromTime and EffectiveToTime . RmsOelEntryUnlinkedPersonInputDtoRmsOelEntryUnlinkedAddressInputDto均从RmsOelEntryInvolvedEntityBaseDto继承,并且此基类具有EffectiveFromTimeEffectiveToTime属性。

I don't want the to have to repeatedly map EffectiveFromTime and EffectiveToTime as seen above. 我不希望必须如上所述反复映射EffectiveFromTimeEffectiveToTime

However, AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedPersonUnlinked and AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedAddressUnlinked are autogenerated and don't have a base class. 但是, AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedPersonUnlinkedAddNewOELEntryInvolvedEntitiesUnlinkedInvolvedAddressUnlinked是自动生成的,没有基类。 Therefore I don't see how I can use AutoMapper's "Include" mapping option. 因此,我看不到如何使用AutoMapper的“包含”映射选项。

How do I optimise this to remove the repeated mapping? 如何优化此设置以删除重复的映射?

I had similar situation and have resorted to helper Extension methods. 我有类似的情况,并求助于辅助扩展方法。 I customized for your case : 我为您量身定做:

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

Then configuration would look like this : 然后配置如下所示:

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

cfg.CreateMap<RmsOelEntryUnlinkedAddressInputDto, AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedAddressUnlinked>()
    .MapCommonFields();

If AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedAddressUnlinked and AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedPersonUnlinked are autogenerated I would assume they are partial. 如果自动生成AddNewOELEntryInvolvedEntitiesUnlinkedInvolvedAddressUnlinkedAddNewOELEntryInvolvedEntitiesUnlinkedInvolvedPersonUnlinked是自动生成的,那么我认为它们是局部的。

Then you could make common interface for those classes : 然后,您可以为这些类创建通用接口:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM