AutoMapper — inheritance mapping not working, same source, multiple destinations

前端 未结 2 511
挽巷
挽巷 2020-12-31 10:12

Can I use inheritance mapping in AutoMapper (v2.2) for maps with the same Source type but different Destination types?

I have this basic situation (the real classes

2条回答
  •  滥情空心
    2020-12-31 10:34

    Yo can do like here

                CreateMap()
                .InheritMapping(x =>
                {
                    x.IncludeDestinationBase();
                });
    

    There is code of extension

    public static class MapExtensions
    {        
    
        public static void InheritMapping(
            this IMappingExpression mappingExpression,
            Action> action)
        {
            InheritMappingExpresssion x =
                new InheritMappingExpresssion(mappingExpression);
            action(x);
            x.ConditionsForAll();
        }
    
        private static bool NotAlreadyMapped(Type sourceType, Type desitnationType, ResolutionContext r, Type typeSourceCurrent, Type typeDestCurrent)
        {
            var result = !r.IsSourceValueNull &&
                   Mapper.FindTypeMapFor(sourceType, desitnationType).GetPropertyMaps().Where(
                       m => m.DestinationProperty.Name.Equals(r.MemberName)).Select(y => !y.IsMapped()
                       ).All(b => b);
            return result;
        }
        public class InheritMappingExpresssion
        {
            private readonly IMappingExpression _sourcExpression;
            public InheritMappingExpresssion(IMappingExpression sourcExpression)
            {
                _sourcExpression = sourcExpression;
            }
            public void IncludeSourceBase(
                bool ovverideExist = false)
            {
                Type sourceType = typeof (TSourceBase);
                Type destinationType = typeof (TDestination);
                if (!sourceType.IsAssignableFrom(typeof (TSource))) throw new NotSupportedException();
                Result(sourceType, destinationType);
            }
            public void IncludeDestinationBase()
            {
                Type sourceType = typeof (TSource);
                Type destinationType = typeof (TDestinationBase);
                if (!destinationType.IsAssignableFrom(typeof (TDestination))) throw new NotSupportedException();
                Result(sourceType, destinationType);
            }
            public void IncludeBothBases()
            {
                Type sourceType = typeof (TSourceBase);
                Type destinationType = typeof (TDestinatioBase);
                if (!sourceType.IsAssignableFrom(typeof (TSource))) throw new NotSupportedException();
                if (!destinationType.IsAssignableFrom(typeof (TDestination))) throw new NotSupportedException();
                Result(sourceType, destinationType);
            }
            internal void ConditionsForAll()
            {
                _sourcExpression.ForAllMembers(x => x.Condition(r => _conditions.All(c => c(r))));//указываем что все кондишены истинны
            }
            private List> _conditions = new List>();
            private void Result(Type typeSource, Type typeDest)
            {
                    _sourcExpression.BeforeMap((x, y) =>
                    {
                        Mapper.Map(x, y, typeSource, typeDest);
                    });
                    _conditions.Add((r) => NotAlreadyMapped(typeSource, typeDest, r, typeof (TSource), typeof (TDestination)));
            }
        }
    
    }
    

提交回复
热议问题