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))); }
public static void InheritMappingFromBaseType <TSource, TDestination>(this IMappingExpression <TSource, TDestination> mappingExpression) { var sourceType = typeof(TSource); var desctinationType = typeof(TDestination); var sourceParentType = sourceType.BaseType; var destinationParentType = desctinationType.BaseType; mappingExpression.BeforeMap((source, destination) => Mapper.Map(source, destination, sourceParentType, destinationParentType)).ForAllMembers(mce => mce.Condition(rc => NotAlreadyMapped(sourceParentType, destinationParentType, rc))); }
public static IMappingExpression <A, B> RegisterChainOfTypes <A, B>(this IMappingExpression <A, B> mapping) { mapping.BeforeMap((a, b, ctx) => { ctx.PushTypeInChainOfTypes(typeof(A)); }); mapping.AfterMap((a, b, ctx) => { ctx.PopLastTypeInChainOfTypes(); }); return(mapping); }
/// <summary> /// 映射 /// </summary> /// <typeparam name="TSource">源实例类型</typeparam> /// <typeparam name="TTarget">目标实例类型</typeparam> /// <param name="sourceInstance">源实例</param> /// <param name="beforeMapEventHandler">映射前事件处理者</param> /// <param name="afterMapEventHandler">映射后事件处理者</param> /// <param name="ignoreMembers">忽略映射成员集</param> /// <returns>目标实例</returns> public static TTarget Map <TSource, TTarget>(this TSource sourceInstance, Action <TSource, TTarget> beforeMapEventHandler = null, Action <TSource, TTarget> afterMapEventHandler = null, params Expression <Func <TTarget, object> >[] ignoreMembers) { #region # 验证 if (sourceInstance == null) { return(default(TTarget)); } #endregion lock (_Sync) { string key = $"{typeof(TSource).FullName},{typeof(TTarget).FullName}"; if (!_MapperConfigurations.TryGetValue(key, out MapperConfiguration mapperConfiguration)) { MapperConfigurationExpression configExpression = new MapperConfigurationExpression(); IMappingExpression <TSource, TTarget> mappingExpression = configExpression.CreateMap <TSource, TTarget>(); #region # 忽略映射成员处理 foreach (Expression <Func <TTarget, object> > ignoreMember in ignoreMembers) { mappingExpression.ForMember(ignoreMember, source => source.Ignore()); } #endregion #region # 映射前后事件处理 if (beforeMapEventHandler != null) { mappingExpression.BeforeMap(beforeMapEventHandler); } if (afterMapEventHandler != null) { mappingExpression.AfterMap(afterMapEventHandler); } #endregion mapperConfiguration = new MapperConfiguration(configExpression); _MapperConfigurations.Add(key, mapperConfiguration); } IMapper mapper = new AutoMapper.Mapper(mapperConfiguration); return(mapper.Map <TTarget>(sourceInstance)); } }
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); }
/// <summary> /// 映射 /// </summary> /// <param name="sourceInstance">源实例</param> /// <param name="beforeMapEventHandler">映射前事件处理者</param> /// <param name="afterMapEventHandler">映射后事件处理者</param> /// <param name="ignoreMembers">忽略映射成员集</param> /// <returns>目标实例</returns> public static TTarget Map(TSource sourceInstance, Action <TSource, TTarget> beforeMapEventHandler = null, Action <TSource, TTarget> afterMapEventHandler = null, params Expression <Func <TTarget, object> >[] ignoreMembers) { #region # 验证参数 if (sourceInstance == null) { return(default(TTarget)); } #endregion MapperConfigurationExpression config = new MapperConfigurationExpression(); IMappingExpression <TSource, TTarget> mapConfig = config.CreateMap <TSource, TTarget>(); #region # 忽略映射成员处理 foreach (Expression <Func <TTarget, object> > ignoreMember in ignoreMembers) { mapConfig.ForMember(ignoreMember, source => source.Ignore()); } #endregion #region # 映射前后事件处理 if (beforeMapEventHandler != null) { mapConfig.BeforeMap(beforeMapEventHandler); } if (afterMapEventHandler != null) { mapConfig.AfterMap(afterMapEventHandler); } #endregion IMapper mapper = new Mapper(new MapperConfiguration(config)); return(mapper.Map <TTarget>(sourceInstance)); }
public virtual void Configure() { Mapping .BeforeMap(BeforeMap) .AfterMap(AfterMap); }
public MappingDestination <TSource, TDestination> BeforeMap(Action <TSource, TDestination> beforeFunction) { Mapping.BeforeMap(beforeFunction); return(this); }