コード例 #1
0
 public static IMapperConfigurationExpression IgnoreMappingOnNullOrIgnoredValue(this IMapperConfigurationExpression expression)
 {
     expression.ForAllPropertyMaps(map =>
     {
         Type sourceType = map.TypeMap.SourceType;
         Type destType   = map.TypeMap.DestinationType;
         return(sourceType.IsSubclassOf(typeof(DtoBase)) && !destType.IsSubclassOf(typeof(DtoBase)));
     },
                                   (map, configuration) =>
     {
         var prop = map.SourceMember as PropertyInfo;
         if (prop != null)
         {
             configuration.Condition((src, dest, srcMember, destMember) =>
             {
                 IgnoreMappingIfValueAttribute attribute = map.SourceMember.GetCustomAttributes().OfType <IgnoreMappingIfValueAttribute>().SingleOrDefault();
                 if (attribute != null)
                 {
                     return(!Object.Equals(srcMember, attribute.IgnoredValue));
                 }
                 else
                 {
                     return(prop.GetValue(src) != null && srcMember != null);
                 }
             });
         }
     }
                                   );
     return(expression);
 }
コード例 #2
0
        public static IMapperConfigurationExpression IgnoreRetrievingAttributeForActionProperty(this IMapperConfigurationExpression expression)
        {
            expression.ForAllPropertyMaps(map => {
                Type sourceType = map.TypeMap.SourceType;
                Type destType   = map.TypeMap.DestinationType;
                if (!sourceType.IsSubclassOf(typeof(DtoBase)) && destType.IsSubclassOf(typeof(DtoBase)) && map.DestinationProperty != null)
                {
                    return(map.DestinationProperty.GetCustomAttributes().OfType <IgnoreRetrievingForAttribute>().Any());
                }
                return(false);
            },
                                          (map, configuration) =>
            {
                Type sourceType = map.TypeMap.SourceType;
                Type destType   = map.TypeMap.DestinationType;

                configuration.PreCondition((opts) =>
                {
                    ActionFlags actions = opts.Options.GetItem("ActionFlags", ActionFlags.Get | ActionFlags.List);
                    IgnoreRetrievingForAttribute attribute = map.DestinationProperty.GetCustomAttributes().OfType <IgnoreRetrievingForAttribute>().SingleOrDefault();
                    if (attribute != null)
                    {
                        if (attribute.actions.HasFlag(actions))
                        {
                            return(false);
                        }
                    }
                    return(true);
                });
            }
                                          );
            return(expression);
        }
コード例 #3
0
 public static void AddIgnoreMapAttribute(this IMapperConfigurationExpression configuration)
 {
     configuration.ForAllMaps((typeMap, mapExpression) => mapExpression.ForAllMembers(memberOptions =>
     {
         if (memberOptions.DestinationMember.Has <IgnoreMapAttribute>())
         {
             memberOptions.Ignore();
         }
     }));
     configuration.ForAllPropertyMaps(propertyMap => propertyMap.SourceMember?.Has <IgnoreMapAttribute>() == true,
                                      (_, memberOptions) => memberOptions.Ignore());
 }
コード例 #4
0
        public virtual void Configure(IMapperConfigurationExpression mapperConfigExpression)
        {
            mapperConfigExpression.CreateMissingTypeMaps = true;

            mapperConfigExpression.ForAllPropertyMaps(p => p.DestinationProperty.GetCustomAttribute <ForeignKeyAttribute>() != null &&
                                                      p.DestinationProperty.GetCustomAttribute <InversePropertyAttribute>() != null &&
                                                      !typeof(IEnumerable).IsAssignableFrom(p.DestinationProperty.ReflectedType) &&
                                                      typeof(IDto).IsAssignableFrom(p.DestinationProperty.ReflectedType),
                                                      (pConfig, member) =>
            {
                pConfig.Ignored = true;
            });
        }
コード例 #5
0
        public virtual void Configure(IMapperConfigurationExpression mapperConfigExpression)
        {
            bool MapperPropConfigurationCondition(PropertyMap p)
            {
                return((p.DestinationMember.GetCustomAttribute <ForeignKeyAttribute>() != null || p.DestinationMember.GetCustomAttribute <InversePropertyAttribute>() != null) &&
                       !typeof(IEnumerable).IsAssignableFrom(p.DestinationMember.ReflectedType) &&
                       typeof(IDto).IsAssignableFrom(p.DestinationMember.ReflectedType));
            }

            mapperConfigExpression.ForAllPropertyMaps(MapperPropConfigurationCondition, (p, member) =>
            {
                p.Ignored = true;
            });
        }
コード例 #6
0
        public static void AddProfile(IMapperConfigurationExpression cfg)
        {
            bool IsToRepeatedField(PropertyMap pm)
            {
                if (pm.DestinationPropertyType.IsConstructedGenericType)
                {
                    var destGenericBase = pm.DestinationPropertyType.GetGenericTypeDefinition();
                    return(destGenericBase == typeof(RepeatedField <>));
                }
                return(false);
            }

            cfg.ForAllPropertyMaps(IsToRepeatedField, (propertyMap, opts) => opts.UseDestinationValue());

            cfg.CreateMap <string, string>().ConvertUsing((src, dest) => src ?? string.Empty);
        }
コード例 #7
0
        public virtual void Configure(IMapperConfigurationExpression mapperConfigExpression)
        {
            mapperConfigExpression.ForAllPropertyMaps(p =>
            {
                var type = p.DestinationType;

                if (type != typeof(string) && typeof(IEnumerable).IsAssignableFrom(type))
                {
                    type = type.HasElementType ? type.GetElementType() : type.GetGenericArguments().ExtendedSingle($"Getting element type of {p.DestinationName}");
                }

                return(typeof(IDto).IsAssignableFrom(type));
            }, (p, conf) =>
            {
                conf.ExplicitExpansion();
            });
        }
コード例 #8
0
 /// <summary>
 /// 此扩展方法是判断属性为Null时不进行属性映射
 /// </summary>
 /// <param name="cfg"></param>
 public static void IgnoreSourceWhenNull(this IMapperConfigurationExpression cfg)
 {
     cfg.ForAllPropertyMaps(pm =>
     {
         if (pm.SourceMember != null && (pm.DestinationMember.Name != pm.SourceMember.Name))
         {
             return(false);
         }
         if (pm.SourceType == null)
         {
             return(false);
         }
         var isNullable = pm.SourceType.IsGenericType && (pm.SourceType.GetGenericTypeDefinition() == typeof(Nullable <>));
         return(isNullable || pm.SourceType.IsValueType || pm.SourceType.IsPrimitive);
     }, (pm, c) =>
     {
         c.MapFrom <Resolver, object>(pm.SourceMember.Name);
     });
 }