コード例 #1
0
        static void ignorePropertiesWithIdsMappingToEntities(IMappingExpression mapping, Type source, Type destination)
        {
            var idTypes = new[] { typeof(Guid), typeof(Guid?) };

            var destinationProps = propertiesOf(destination).ToDictionary(pi => pi.Name);

            var sourceProps            = propertiesOf(source);
            var whereThePropertyIsAnId = sourceProps.Where(p => idTypes.Contains(p.PropertyType));
            var withDestination        = whereThePropertyIsAnId.Select(SourceProp => new { SourceProp, DestinationProp = destinationProps.TryGetValue(SourceProp.Name) });
            var andTheTargetIsAnEntity = withDestination.Where(x => x.DestinationProp?.PropertyType.IsAssignableTo <IEntity>() ?? false);

            foreach (var sourceProp in andTheTargetIsAnEntity.Select(x => x.SourceProp.Name))
            {
                mapping.ForMember(sourceProp, x => x.Ignore());
            }
        }
コード例 #2
0
ファイル: ObjectProxyFactory.cs プロジェクト: jseijas/supido
        public static void CreateMap(Type a, Type b, IList <string> sources, IList <string> targets)
        {
            IMappingExpression expressionA2B = Mapper.CreateMap(a, b);
            IMappingExpression expressionB2A = Mapper.CreateMap(b, a);

            if (sources != null && targets != null)
            {
                for (int i = 0; i < sources.Count; i++)
                {
                    string sourceName = sources[i];
                    string targetName = targets[i];
                    expressionA2B.ForMember(targetName, opt => opt.MapFrom(sourceName));
                    expressionB2A.ForMember(sourceName, opt => opt.MapFrom(targetName));
                }
            }
        }
 public static IMappingExpression <TSource, TDestination> MapBaseContentTypeSaveToDisplay <TSource, TPropertyTypeSource, TDestination, TPropertyTypeDestination>(
     this IMappingExpression <TSource, TDestination> mapping)
     where TSource : ContentTypeSave <TPropertyTypeSource>
     where TDestination : ContentTypeCompositionDisplay <TPropertyTypeDestination>
     where TPropertyTypeDestination : PropertyTypeDisplay
     where TPropertyTypeSource : PropertyTypeBasic
 {
     return(mapping
            .ForMember(dto => dto.CreateDate, expression => expression.Ignore())
            .ForMember(dto => dto.UpdateDate, expression => expression.Ignore())
            .ForMember(dto => dto.ListViewEditorName, expression => expression.Ignore())
            .ForMember(dto => dto.Notifications, expression => expression.Ignore())
            .ForMember(dto => dto.Errors, expression => expression.Ignore())
            .ForMember(dto => dto.LockedCompositeContentTypes, exp => exp.Ignore())
            .ForMember(dto => dto.Groups, expression => expression.ResolveUsing(new PropertyGroupDisplayResolver <TSource, TPropertyTypeSource, TPropertyTypeDestination>())));
 }
コード例 #4
0
        public static IMappingExpression <TSource, TDestination> IgnoreAllNonExisting <TSource, TDestination>(this IMappingExpression <TSource, TDestination> expression)
        {
            const BindingFlags flags  = BindingFlags.Public | BindingFlags.Instance;
            var sourceType            = typeof(TSource);
            var destinationProperties = typeof(TDestination).GetProperties(flags);

            foreach (var property in destinationProperties)
            {
                var propInfoSrc = sourceType.GetProperties().FirstOrDefault(p => p.Name == property.Name);
                if (propInfoSrc == null)
                {
                    expression.ForMember(property.Name, opt => opt.Ignore());
                }
            }
            return(expression);
        }
コード例 #5
0
    public static IMappingExpression <TSource, TDestination> IgnoreReadOnly <TSource, TDestination>(
        this IMappingExpression <TSource, TDestination> expression)
    {
        var destType = typeof(TDestination);

        foreach (var property in destType.GetProperties())
        {
            PropertyDescriptor descriptor = TypeDescriptor.GetProperties(destType)[property.Name];
            ReadOnlyAttribute  attribute  = (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];
            if (attribute.IsReadOnly == true)
            {
                expression.ForMember(property.Name, opt => opt.Ignore());
            }
        }
        return(expression);
    }
コード例 #6
0
        public static IMappingExpression IgnoreAllNonExisting(this IMappingExpression expression)
        {
            var mappingExpression     = (MappingExpression)expression;
            var flags                 = BindingFlags.Public | BindingFlags.Instance;
            var destinationProperties = mappingExpression.Types.DestinationType.GetProperties(flags);

            foreach (var property in destinationProperties)
            {
                if (mappingExpression.Types.SourceType.GetProperty(property.Name, flags) != null)
                {
                    continue;
                }
                expression.ForMember(property.Name, opt => opt.Ignore());
            }
            return(expression);
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: Nohovich/.NET
        public static IMappingExpression <TSource, TDestination> IgnoreNoMap <TSource, TDestination>(
            this IMappingExpression <TSource, TDestination> expression)
        {
            var sourceType = typeof(TSource);

            foreach (var property in sourceType.GetProperties())
            {
                PropertyDescriptor descriptor = TypeDescriptor.GetProperties(sourceType)[property.Name];
                NoMapAttribute     attribute  = (NoMapAttribute)descriptor.Attributes[typeof(NoMapAttribute)];
                if (attribute != null)
                {
                    expression.ForMember(property.Name, opt => opt.Ignore());
                }
            }
            return(expression);
        }
コード例 #8
0
        public static IMappingExpression <TServiceModel, TModel> MapReference <TServiceModel, TServiceModelProperty, TModel, TModelProperty>(
            this IMappingExpression <TServiceModel, TModel> mappingExpression,
            Expression <Func <TServiceModel, IList <TServiceModelProperty> > > sourceMember,
            Expression <Func <TModel, IList <TModelProperty> > > destinationMember)
            where TServiceModel : ServiceModelBase
            where TServiceModelProperty : ServiceModelBase
            where TModel : ModelBase
            where TModelProperty : ModelBase
        {
            // in stead of mapping the source property to the destination property, try to retrieve the already stored destination object from the database
            mappingExpression = mappingExpression.ForMember(
                destinationMember,
                options => options.ResolveUsing((serviceModel, model, modelProperty, resolutionContext) => serviceModel.FetchDestination(model, resolutionContext, sourceMember, destinationMember)));

            return(mappingExpression);
        }
コード例 #9
0
        public static IMappingExpression <TSource, TDestination> IgnoreAllNonExisting <TSource, TDestination>
            (this IMappingExpression <TSource, TDestination> expression)
        {
            var flags                 = BindingFlags.Public | BindingFlags.Instance;
            var sourceType            = typeof(TSource);
            var destinationProperties = typeof(TDestination).GetProperties(flags);

            foreach (var property in destinationProperties)
            {
                if (sourceType.GetProperty(property.Name, flags) == null)
                {
                    expression.ForMember(property.Name, opt => opt.Ignore());
                }
            }
            return(expression);
        }
コード例 #10
0
        /// <summary>
        /// Instructs the AutoMapper profile to resolve the specified <paramref name="constructorParam"/> by checking the resolution context for an
        /// already created item matching the <paramref name="keyAttributes"/> of the source's reference property.
        /// </summary>
        /// <param name="expression">
        /// The expression.
        /// </param>
        /// <param name="resolutionSource">
        /// The resolution source.
        /// </param>
        /// <param name="resolutionTarget">
        /// The resolution target.
        /// </param>
        /// <param name="constructorParam">
        /// The constructor param.
        /// </param>
        /// <param name="keyAttributes">
        /// The key attributes.
        /// </param>
        /// <typeparam name="TSource">
        /// The type of item that is the source of the mapping expression.
        /// </typeparam>
        /// <typeparam name="TReference">
        /// The type of property on the source that will be used as the reference to resolve on the target.
        /// </typeparam>
        /// <typeparam name="TDest">
        /// The type of item that is the destination of the mapping expression.
        /// </typeparam>
        /// <typeparam name="TTarget">
        /// The type of property on the destination that will be resolved.
        /// </typeparam>
        /// <returns>
        /// The current <see cref="IMappingExpression{TSource,TDest}"/>.
        /// </returns>
        public static IMappingExpression <TSource, TDest> ResolveByKey <TSource, TReference, TDest, TTarget>(
            [NotNull] this IMappingExpression <TSource, TDest> expression,
            [NotNull] Expression <Func <TSource, TReference> > resolutionSource,
            [NotNull] Expression <Func <TDest, TTarget> > resolutionTarget,
            [NotNull] string constructorParam,
            [NotNull] params Expression <Func <TReference, object> >[] keyAttributes)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            if (resolutionSource == null)
            {
                throw new ArgumentNullException(nameof(resolutionSource));
            }

            if (resolutionTarget == null)
            {
                throw new ArgumentNullException(nameof(resolutionTarget));
            }

            if (constructorParam == null)
            {
                throw new ArgumentNullException(nameof(constructorParam));
            }

            if (keyAttributes == null)
            {
                throw new ArgumentNullException(nameof(keyAttributes));
            }

            expression.ForCtorParam(
                constructorParam,
                configurationExpression => configurationExpression.MapFrom(
                    (source, context) => ResolveItem <TReference, TTarget>(context, resolutionSource.Compile().Invoke(source), keyAttributes.ToArray())));

            expression.ForMember(
                resolutionTarget,
                configurationExpression => configurationExpression.MapFrom(
                    (source, dest, target, context) => ResolveItem <TReference, TTarget>(
                        context,
                        resolutionSource.Compile().Invoke(source),
                        keyAttributes.ToArray())));

            return(expression);
        }
コード例 #11
0
    public static IMappingExpression <TSource, TDestination> InheritMappingFromBaseType <TSource, TDestination>(
        this IMappingExpression <TSource, TDestination> mappingExpression,
        WithBaseFor baseFor          = WithBaseFor.Both,
        IMappingEngine mappingEngine = null,
        IConfigurationProvider configurationProvider = null)
    {
        Type sourceType       = typeof(TSource);
        Type destinationType  = typeof(TDestination);
        Type sourceParentType = baseFor == WithBaseFor.Both || baseFor == WithBaseFor.Source
                ? sourceType.BaseType
                : sourceType;
        Type destinationParentType = baseFor == WithBaseFor.Both || baseFor == WithBaseFor.Destination
                ? destinationType.BaseType
                : destinationType;

        mappingExpression
        .BeforeMap((sourceObject, destObject) =>
        {
            if (mappingEngine != null)
            {
                mappingEngine.Map(sourceObject, destObject, sourceParentType, destinationParentType);
            }
            else
            {
                Mapper.Map(sourceObject, destObject, sourceParentType, destinationParentType);
            }
        });
        TypeMap baseTypeMap = configurationProvider != null
                ? configurationProvider.FindTypeMapFor(sourceParentType, destinationParentType)
                : Mapper.FindTypeMapFor(sourceParentType, destinationParentType);

        if (baseTypeMap == null)
        {
            throw new InvalidOperationException(
                      string.Format("Missing map from {0} to {1}.", new object[]
            {
                sourceParentType.Name,
                destinationParentType.Name
            }));
        }
        foreach (PropertyMap propertyMap in baseTypeMap.GetPropertyMaps())
        {
            mappingExpression.ForMember(propertyMap.DestinationProperty.Name, opt => opt.Ignore());
        }
        return(mappingExpression);
    }
コード例 #12
0
        /// <summary>
        /// Map destination member using destination and source member expressions
        /// </summary>
        /// <typeparam name="TDest">Destination type</typeparam>
        /// <typeparam name="TSource">Source type</typeparam>
        /// <param name="mappingExpression">IMappingExpression</param>
        /// <param name="destinationMember">Destination member</param>
        /// <param name="sourceMember">Source member</param>
        /// <param name="memberOptions">Member options</param>
        /// <returns>Itself</returns>
        public static IMappingExpression <TSource, TDest> MapTo <TDest, TSource>(this IMappingExpression <TSource, TDest> mappingExpression,
                                                                                 Expression <Func <TDest, object?> > destinationMember,
                                                                                 Expression <Func <TSource, object?> >?sourceMember = null,
                                                                                 Action <IMemberConfigurationExpression <TSource, TDest, object?> >?memberOptions = null)
        {
            if (destinationMember is null)
            {
                throw new AutoMapperConfigurationException("Destination member expression cannot be null");
            }

            mappingExpression.ForMember(destinationMember, o =>
            {
                o.MapFrom(sourceMember ?? (s => s));
                memberOptions?.Invoke(o);
            });
            return(mappingExpression);
        }
コード例 #13
0
        private static IMappingExpression <TSource, TDestination> Merge <TSource, TDestination>
            (IMappingExpression <TSource, TDestination> mapping)
        {
            var type = typeof(TSource);

            foreach (var property in type.GetProperties())
            {
                mapping.ForMember(property.Name, action =>
                                  action.Condition(src =>
                                                   type.IsValueType
                            ? property.GetValue(src).Equals(Activator.CreateInstance(type))
                            : property.GetValue(src) != null
                                                   ));
            }

            return(mapping);
        }
コード例 #14
0
        public static IMappingExpression MapDisplayValues(this IMappingExpression expression, Type sourceType, Type destinationType)
        {
            //List<string> prefixes = new List<string> { "GENCAT_", "PERCAT_", "INSCAT_" };
            const string DISPLAY_SUFFIX   = "Display";
            const string DISPLAY_PROPERTY = ".Descripcion";

            var entityProperties = destinationType.GetProperties().Where(p => p.PropertyType == typeof(string) && p.Name.EndsWith(DISPLAY_SUFFIX));

            foreach (var prop in entityProperties)
            {
                var destPropertyName   = prop.Name;
                var sourcePropertyName = prop.Name.Replace(DISPLAY_SUFFIX, string.Empty).ToUpper() + DISPLAY_PROPERTY;
                //var destPropertyName = prefixes.GENCAT + prop.Name.ToUpper() ;
                expression.ForMember(destPropertyName, map => map.MapFrom(sourcePropertyName));
            }
            return(expression);
        }
コード例 #15
0
        public static IMappingExpression <TSource, TDestination> IgnoreAllUnmapped <TSource, TDestination>(this IMappingExpression <TSource, TDestination> expression)
        {
            BindingFlags flags      = BindingFlags.Public | BindingFlags.Instance;
            Type         sourceType = typeof(TSource);

            PropertyInfo[] destinationProperties = typeof(TDestination).GetProperties(flags);

            foreach (PropertyInfo property in destinationProperties)
            {
                if (sourceType.GetProperty(property.Name, flags) == null)
                {
                    expression.ForMember(property.Name, o => o.Ignore());
                }
            }

            return(expression);
        }
コード例 #16
0
        public static void IngoreNotMapped <TSource, TDestination>(this IMappingExpression <TSource, TDestination> mappingExpression)
        {
            if (mappingExpression is null)
            {
                throw new ArgumentNullException(nameof(mappingExpression));
            }

            var destprops = typeof(TDestination).GetProperties();

            foreach (PropertyInfo item in destprops)
            {
                if (item.IsDefined(typeof(AutoNotMapAttribute), true))
                {
                    mappingExpression.ForMember(item.Name, opt => opt.Ignore());
                }
            }
        }
コード例 #17
0
        /// <summary>
        /// 设定除指定字段外的其他所有属性的值
        /// </summary>
        /// <typeparam name="TSource">source</typeparam>
        /// <typeparam name="TKey">key</typeparam>
        /// <typeparam name="TDestination">dto</typeparam>
        /// <param name="destinationMember">member(dto)</param>
        /// <param name="expression">keys</param>
        /// <returns></returns>
        public static IMappingExpression <TSource, TDestination> SetWithoutProperties <TSource, TKey, TDestination>(this IMappingExpression <TSource, TDestination> destinationMember, params Expression <Func <TSource, TKey> >[] expression)
        {
            return(destinationMember);

            var withoutFields = lambdaFields(expression);

            var destProperties = typeof(TDestination)
                                 .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                 .Where(w => w.CanRead && w.CanWrite && withoutFields.Contains(w.Name));

            foreach (var destProperty in destProperties)
            {
                destinationMember.ForMember(destProperty.Name, opts => opts.Ignore());
            }

            return(destinationMember);
        }
コード例 #18
0
        /// <summary>
        /// Map destination member using name and source member expressions
        /// </summary>
        /// <typeparam name="TDest">Destination type</typeparam>
        /// <typeparam name="TSource">Source type</typeparam>
        /// <param name="mappingExpression">IMappingExpression</param>
        /// <param name="destinationMember">Destination member</param>
        /// <param name="sourceMember">Source member</param>
        /// <param name="memberOptions">Member options</param>
        /// <returns>Itself</returns>
        public static IMappingExpression <TSource, TDest> MapTo <TDest, TSource>(this IMappingExpression <TSource, TDest> mappingExpression,
                                                                                 string destinationMember,
                                                                                 Expression <Func <TSource, object?> >?sourceMember = null,
                                                                                 Action <IMemberConfigurationExpression <TSource, TDest, object?> >?memberOptions = null)
        {
            if (string.IsNullOrWhiteSpace(destinationMember))
            {
                throw new AutoMapperConfigurationException("Destination member name cannot be null or empty");
            }

            mappingExpression.ForMember(destinationMember, o =>
            {
                o.MapFrom(sourceMember ?? (s => s));
                memberOptions?.Invoke(o);
            });
            return(mappingExpression);
        }
コード例 #19
0
        public static IMappingExpression <TServiceModel, TModel> MapReference <TServiceModel, TServiceModelProperty, TModel, TModelProperty>(
            this IMappingExpression <TServiceModel, TModel> mappingExpression,
            Expression <Func <TServiceModel, TServiceModelProperty> > sourceMember,
            Expression <Func <TModel, TModelProperty> > destinationMember, bool ignoreNull = false)
            where TServiceModel : ServiceModelBase
            where TServiceModelProperty : ServiceModelBase
            where TModel : ModelBase
            where TModelProperty : ModelBase
        {
            // in stead of mapping the source property to the destination property, try to retrieve the already stored destination object from the database
            mappingExpression = mappingExpression.ForMember(
                destinationMember,
                options => options.ResolveUsing((serviceModel, model, modelProperty, resolutionContext) =>
            {
                if (ignoreNull)
                {
                    var func        = sourceMember.Compile();
                    var sourceValue = func(serviceModel);

                    if (sourceValue == null)
                    {
                        return(modelProperty);
                    }
                }

                var value = serviceModel.FetchDestination <TServiceModel, TModelProperty, TServiceModelProperty>(
                    resolutionContext, sourceMember);

                if (value == null)
                {
                    var dataContext = GetDbContext(resolutionContext);
                    var entry       = dataContext.Entry(model);

                    if (entry.State != EntityState.Detached)
                    {
                        entry.Reference(destinationMember.GetPropertyName()).Load();
                        model.SetPropertyValue(destinationMember, null);
                    }
                }

                return(value);
            }));

            return(mappingExpression);
        }
コード例 #20
0
        public static IMappingExpression <TSource, TDestination> MapBaseContentTypeSaveToDisplay <TSource, TPropertyTypeSource, TDestination, TPropertyTypeDestination>(
            this IMappingExpression <TSource, TDestination> mapping)
            where TSource : ContentTypeSave <TPropertyTypeSource>
            where TDestination : ContentTypeCompositionDisplay <TPropertyTypeDestination>
            where TPropertyTypeDestination : PropertyTypeDisplay
            where TPropertyTypeSource : PropertyTypeBasic
        {
            var propertyGroupDisplayResolver = new PropertyGroupDisplayResolver <TSource, TPropertyTypeSource, TPropertyTypeDestination>();

            return(mapping
                   .ForMember(dest => dest.CreateDate, opt => opt.Ignore())
                   .ForMember(dest => dest.UpdateDate, opt => opt.Ignore())
                   .ForMember(dest => dest.ListViewEditorName, opt => opt.Ignore())
                   .ForMember(dest => dest.Notifications, opt => opt.Ignore())
                   .ForMember(dest => dest.Errors, opt => opt.Ignore())
                   .ForMember(dest => dest.LockedCompositeContentTypes, opt => opt.Ignore())
                   .ForMember(dest => dest.Groups, opt => opt.MapFrom(src => propertyGroupDisplayResolver.Resolve(src))));
        }
コード例 #21
0
        public static IMappingExpression <TSource, TDestination> LimitStrings <TSource, TDestination>(this IMappingExpression <TSource, TDestination> expression)
        {
            var sourceType      = typeof(TSource);
            var destinationType = typeof(TDestination);
            var existingMaps    = Mapper.Configuration.GetAllTypeMaps().First(b => b.SourceType == sourceType && b.DestinationType == destinationType);
            var propertyMaps    = existingMaps.PropertyMaps.Where(map => !map.Ignored && ((PropertyInfo)map.SourceMember).PropertyType == typeof(string));

            foreach (var propertyMap in propertyMaps)
            {
                var attr = propertyMap.DestinationMember.GetCustomAttribute <MaxLengthAttribute>();
                if (attr != null)
                {
                    expression.ForMember(propertyMap.DestinationMember.Name,
                                         opt => opt.ConvertUsing(new StringLimiter(attr.Length), propertyMap.SourceMember.Name));
                }
            }
            return(expression);
        }
コード例 #22
0
        /// <summary>
        /// 增加映射类到解析清单
        /// </summary>
        /// <typeparam name="T">DAO(数据库实体类)</typeparam>
        /// <typeparam name="T2">DTO(传输实体类)</typeparam>
        /// <param name="members"></param>
        /// <returns></returns>
        private IMappingExpression <T, T2> AddMapper <T, T2>(string members = null)
        {
            IMappingExpression <T, T2> profile = _config.CreateMap <T, T2>(MemberList.None);

            if (string.IsNullOrWhiteSpace(members))
            {
                return(profile);
            }
            string[] memberSet = members.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string member in memberSet)
            {
                string[] memberExp = member.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                string   memberDTO = memberExp.First();
                string   memberDAO = memberExp.Last();
                profile.ForMember(memberDTO, _ => _.MapFrom(memberDAO));
            }
            ;
            return(profile);
        }
コード例 #23
0
 /// <summary>
 /// 忽略 目标对象 所有 存在于ignores中的字段
 /// </summary>
 /// <param name="mapping"></param>
 /// <param name="ignores">需要忽略的字段</param>
 /// <returns></returns>
 internal static IMappingExpression IgnoreAllExisting(this IMappingExpression mapping, IReadOnlyList <string> ignores)
 {
     if (mapping == null)
     {
         return(mapping);
     }
     //获取需要忽略的字段
     if (ignores != null && ignores.Count() > 0)
     {
         foreach (var item in ignores)
         {
             if (!string.IsNullOrWhiteSpace(item))
             {
                 mapping.ForMember(item, opton => opton.Ignore());
             }
         }
     }
     return(mapping);
 }
コード例 #24
0
        private static void IgnoreUnmappedProperties(TypeMap map, IMappingExpression expr)
        {
            foreach (string propName in map.GetUnmappedPropertyNames())
            {
                var srcPropInfo = map.SourceType.GetProperty(propName);

                if (srcPropInfo != null)
                {
                    expr.ForSourceMember(propName, opt => opt.DoNotValidate());
                }

                var destPropInfo = map.DestinationType.GetProperty(propName);

                if (destPropInfo != null)
                {
                    expr.ForMember(propName, opt => opt.Ignore());
                }
            }
        }
コード例 #25
0
        public static IMappingExpression <S, D> MapMember <S, D, TTo, TMember>(
            this IMappingExpression <S, D> map,
            Expression <Func <D, TTo> > to,
            Expression <Func <S, TMember> > from,
            Action <IMemberConfigurationExpression <S, D, TTo> > opts = null,
            bool skipEnsureMapping = false
            )
        {
            map.ForMember(to, o =>
            {
                o.MapFrom(from);
                if (opts != null)
                {
                    opts(o);
                }
            });

            return(map);
        }
コード例 #26
0
        public static IMappingExpression <TSource, TDestination> IgnoreChildEntityLists <TSource, TDestination>(
            this IMappingExpression <TSource, TDestination> expression)
        {
            var sourceType = typeof(TSource);

            foreach (var property in sourceType.GetProperties())
            {
                if (property.PropertyType.IsSubtypeOfGeneric(typeof(IList <>)))
                {
                    var genericArgument = property.PropertyType.GenericTypeArguments.FirstOrDefault();

                    if (genericArgument?.GetInterfaces().Contains(typeof(IEntity)) == true)
                    {
                        expression.ForMember(property.Name, opt => opt.Ignore());
                    }
                }
            }
            return(expression);
        }
コード例 #27
0
        public static IMappingExpression <TSource, TDestination> IgnoreReadOnly <TSource, TDestination>(
            this IMappingExpression <TSource, TDestination> expression)
        {
            var sourceType     = typeof(TSource);
            var destProperties = typeof(TDestination).GetProperties();

            foreach (var property in sourceType.GetProperties())
            {
                if (destProperties.Where(x => x.Name == property.Name).FirstOrDefault() != null)
                {
                    PropertyDescriptor descriptor = TypeDescriptor.GetProperties(sourceType)[property.Name];
                    ReadOnlyAttribute  attribute  = (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];
                    if (attribute.IsReadOnly == true)
                    {
                        expression.ForMember(property.Name, opt => opt.Ignore());
                    }
                }
            }
            return(expression);
        }
コード例 #28
0
        /// <summary>
        /// 忽略所有未存在的 Mapping
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TDestination"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static IMappingExpression <TSource, TDestination> IgnoreAllNonExisting <TSource, TDestination>(this IMappingExpression <TSource, TDestination> expression)
        {
            var sourceType      = typeof(TSource);
            var destinationType = typeof(TDestination);

            try
            {
                var existMaps = Mapper.Instance.ConfigurationProvider.GetAllTypeMaps().First(x => x.SourceType.Equals(sourceType) && x.DestinationType.Equals(destinationType));
                foreach (var property in existMaps.GetUnmappedPropertyNames())
                {
                    expression.ForMember(property, opt => opt.Ignore());
                }
            }
            catch
            {
                // Do nothing
            }

            return(expression);
        }
 public static IMappingExpression <TSource, TDest> MapCommon <TSource, TDest>(this IMappingExpression <TSource, TDest> mappingExpression)
     where TSource : PaymentsEvent
     where TDest : PaymentsEventModel
 {
     return(mappingExpression
            .ForMember(dest => dest.EventId, opt => opt.MapFrom(source => source.EventId))
            .ForMember(dest => dest.CollectionPeriod, opt => opt.MapFrom(source => source.CollectionPeriod.Period))
            .ForMember(dest => dest.AcademicYear, opt => opt.MapFrom(source => source.CollectionPeriod.AcademicYear))
            .ForMember(dest => dest.EventTime, opt => opt.MapFrom(source => source.EventTime))
            .ForMember(dest => dest.IlrSubmissionDateTime, opt => opt.MapFrom(source => source.IlrSubmissionDateTime))
            .ForMember(dest => dest.JobId, opt => opt.MapFrom(source => source.JobId))
            .ForMember(dest => dest.LearnerReferenceNumber, opt => opt.MapFrom(source => source.Learner.ReferenceNumber))
            .ForMember(dest => dest.LearnerUln, opt => opt.MapFrom(source => source.Learner.Uln))
            .ForMember(dest => dest.LearningAimPathwayCode, opt => opt.MapFrom(source => source.LearningAim.PathwayCode))
            .ForMember(dest => dest.LearningAimFrameworkCode, opt => opt.MapFrom(source => source.LearningAim.FrameworkCode))
            .ForMember(dest => dest.LearningAimFundingLineType, opt => opt.MapFrom(source => source.LearningAim.FundingLineType))
            .ForMember(dest => dest.LearningAimProgrammeType, opt => opt.MapFrom(source => source.LearningAim.ProgrammeType))
            .ForMember(dest => dest.LearningAimReference, opt => opt.MapFrom(source => source.LearningAim.Reference))
            .ForMember(dest => dest.LearningAimStandardCode, opt => opt.MapFrom(source => source.LearningAim.StandardCode)));
 }
コード例 #30
0
        public static IMappingExpression <TSource, TDestination> IgnoreAllNonExisting <TSource, TDestination>
            (this IMappingExpression <TSource, TDestination> expression)
        {
            var sourceType      = typeof(TSource);
            var destinationType = typeof(TDestination);
            var existingMaps    = expression.TypeMap;

            foreach (var property in existingMaps.GetUnmappedPropertyNames())
            {
                if (sourceType.GetProperty(property) != null)
                {
                    expression.ForSourceMember(property, o => o.Ignore());
                }
                if (destinationType.GetProperty(property) != null)
                {
                    expression.ForMember(property, opt => opt.Ignore());
                }
            }
            return(expression);
        }
コード例 #31
0
ファイル: CellTest.cs プロジェクト: ouyh18/LtePlatform
 private static IMappingExpression MappingCore(IMappingExpression coreMap,
     Type resolveActionType, string destName, string srcName)
 {
     if (resolveActionType == null)
         coreMap = coreMap.ForMember(destName, map => map.MapFrom(srcName));
     else
         coreMap = coreMap.ForMember(destName,
             map => map.ResolveUsing(resolveActionType).FromMember(srcName));
     return coreMap;
 }