public static IMappingExpression <TSource, TDestination> IgnoreDataInfo <TSource, TDestination>( this IMappingExpression <TSource, TDestination> map) where TSource : DataInfoEntityDto where TDestination : DataInfoEntity { map.ForMember(dest => dest.I_CREATE_DATE, config => config.Ignore()); map.ForMember(dest => dest.I_CREATE_BY, config => config.Ignore()); map.ForMember(dest => dest.I_UPDATE_DATE, config => config.ResolveUsing <UpdateDateResolver>().FromMember(x => x.I_UPDATE_DATE)); map.ForMember(dest => dest.I_UPDATE_BY, config => config.ResolveUsing <UpdateByResolver>().FromMember(x => x.I_UPDATE_BY)); map.ForMember(dest => dest.I_STATE, config => config.Ignore()); map.AfterMap((src, dest) => { if (string.IsNullOrEmpty(dest.I_CREATE_BY) && dest.I_CREATE_DATE == null) { if (HttpContext.Current != null && HttpContext.Current.User.Identity.IsAuthenticated) { dest.I_CREATE_BY = HttpContext.Current.User.Identity.Name; } dest.I_CREATE_DATE = DateTime.Now; } }); return(map); }
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)); } }
private IMappingExpression AddCallbacks(IMappingExpression mapping, Type type) { var callbackAttribs = type.GetCustomAttributes <DeserializationCallbackAttribute>(); if (callbackAttribs.Any()) { mapping.AfterMap((src, dst) => { foreach (var attrib in callbackAttribs) { attrib.Callback.OnDeserialized(dst); } }); } return(mapping); }
internal static IMappingExpression <T1, T2> ArmaServerInfoModelToArmaServer <T1, T2>( this IMappingExpression <T1, T2> cfg) where T1 : ArmaServerInfoModel where T2 : ArmaServer => cfg.AfterMap((src, dest) => src.GameTags?.MapTo(dest)) //.ForMember(x => x.Location, opt => opt.ResolveUsing<LocationResolver2>()) .ForMember(x => x.ConnectionAddress, opt => opt.MapFrom(src => src.ConnectionEndPoint)) .ForMember(x => x.QueryAddress, opt => opt.MapFrom(src => src.QueryEndPoint)) .ForMember(x => x.Game, opt => opt.MapFrom(src => TryUrlDecode(src.Mission))) .ForMember(x => x.IsPasswordProtected, opt => opt.MapFrom(src => src.RequirePassword)) .ForMember(x => x.Version, opt => opt.ResolveUsing(src => { if (src.Version == null) { return(null); } Version v; return(Version.TryParse(src.Version, out v) ? v : null); }));
public static IMappingExpression <TSource, TDestination> ForItems <TSource, TDestination, T>( this IMappingExpression <TSource, TDestination> mapper) where TSource : IEnumerable where TDestination : ICollection <T> { mapper.AfterMap((c, s) => { if (c != null && s != null) { foreach (var t in c) { s.Add(Mapper.Map <T>(t)); } } }); return(mapper); }
public static IMappingExpression <TSource, TDestination> IgnoreDataInfoSelfMapping <TSource, TDestination>( this IMappingExpression <TSource, TDestination> map) where TSource : DataInfoEntity where TDestination : DataInfoEntity { map.ForMember(dest => dest.I_CREATE_DATE, config => config.Ignore()); map.ForMember(dest => dest.I_CREATE_BY, config => config.Ignore()); map.ForMember(dest => dest.UNITCODE, config => config.Ignore()); map.AfterMap((src, dest) => { if (string.IsNullOrEmpty(dest.I_CREATE_BY) && dest.I_CREATE_DATE == null) { if (HttpContext.Current != null && HttpContext.Current.User.Identity.IsAuthenticated) { dest.I_CREATE_BY = HttpContext.Current.User.Identity.Name; } dest.I_CREATE_DATE = DateTime.Now; } }); return(map); }
/// <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 static IMappingExpression <TServiceModel, TModel> MapBackReference <TServiceModel, TModel, TChild>( this IMappingExpression <TServiceModel, TModel> mappingExpression, Func <TModel, IList <TChild> > childSelector, Expression <Func <TChild, TModel> > backReferenceProperty) { // get a function for retrieving the backreference value of an object var getValue = backReferenceProperty.Compile(); // fill all missing back references of the items in the collection mappingExpression = mappingExpression.AfterMap((src, dest) => { foreach (var child in childSelector(dest)) { if (getValue(child) == null) { child.SetPropertyValue(backReferenceProperty, dest); } } }); return(mappingExpression); }
public static IMappingExpression <TDto, TEntity> AfterMapSetEntityState <TContext, TDto, TEntity>(this IMappingExpression <TDto, TEntity> expression) where TContext : DbContext where TEntity : class, new() where TDto : DtoBase <TContext, TEntity, TDto>, new() { expression.AfterMap((src, dest, opts) => { var status = (ISuccessOrErrors)opts.Options.Items["CurrentStatus"]; var dbContext = (TContext)opts.Options.Items["DbContext"]; var srcEntity = src.FindItemUntracked(dbContext); if (srcEntity == null) { status.Combine(src.CreateDataFromDto(dbContext, dest)); src.SetupRestOfEntity(dbContext, dest); } else { status.Combine(src.UpdateDataFromDto(dbContext, dest, srcEntity)); src.SetupRestOfEntity(dbContext, dest); } }); return(expression); }
public MappingDestination <TSource, TDestination> AfterMap(Action <TSource, TDestination> afterFunction) { Mapping.AfterMap(afterFunction); return(this); }