protected override void Map(IEnumerable <MapTarget> MapTypes) { foreach (var map in MapTypes) { var src = map.target; var trgt = map.source; if (!MapSetter.IscomplexMapping <IComplexType>(map)) { if (!typeof(ITarget).IsAssignableFrom(map.target)) { CreateMap(src, trgt) .ForSourceMember("Id", opt => opt.Ignore()); } //.ReverseMap(); else if (MapSetter.ContainsColumn(src, "Id")) { CreateMap(src, trgt) .ForSourceMember("Id", opt => opt.Ignore()); } else { CreateMap(src, trgt); } } else { if (MapSetter.ContainsColumn(src, "Id")) { CreateMap(src, trgt) .ForSourceMember("Id", opt => opt.Ignore()) .ProjectUsing(s => MapSetter.EntityDestination <ITarget, IComplexType>(s, trgt)); } else { CreateMap(src, trgt) .ProjectUsing(s => MapSetter.EntityDestination <ITarget, IComplexType>(s, trgt)); } } } }
protected virtual void Map(IEnumerable <MapTarget> MapTypes) { ShouldMapProperty = pi => pi.PropertyType != typeof(IList <>); foreach (var map in MapTypes) { if (!MapSetter.IscomplexMapping <IComplexType>(map) && !map.IsCompexMapping) { if (!typeof(ITarget).IsAssignableFrom(map.target)) { CreateMap(map.source, map.target) .ReverseMap(); } else { CreateMap(map.source, map.target) .ReverseMap(); } } else { if (map.IsCompexMapping) { CreateMap(map.source, map.target, MemberList.None) .ProjectUsing(src => MapSetter.ModelDestination(src, map.target)); CreateMap(map.target, map.source) .ProjectUsing(src => MapSetter.EntityDestination(src, map.source)); } else { CreateMap(map.source, map.target, MemberList.None) .ProjectUsing(src => MapSetter.ModelDestination <ITarget, IComplexType>(src, map.target)); CreateMap(map.target, map.source) .ProjectUsing(src => MapSetter.EntityDestination <ITarget, IComplexType>(src, map.source)); } } } }
/// <summary> /// Initialize mapper /// </summary> public static void Init <ISource, ITarget, IComplexType>(bool forced = false) { if (_mapper != null && forced == false) { return; } _mapper = null; Func <Assembly, IEnumerable <Type> > ValidMapperTypes = (Assembly invalue) => { IEnumerable <Type> types = new List <Type>(); try { types = invalue.GetTypes(); } catch { } return(types); }; Func <Assembly, bool> ValidAssm = (Assembly invalue) => { bool ret = true; try { invalue.GetTypes().Any(); } catch { ret = false; } return(ret); }; var Types = AppDomain .CurrentDomain .GetAssemblies() .SelectMany(s => ValidMapperTypes(s)) .Where(i => !i.GetCustomAttributes(typeof(Atrributes.IgnoreMapAttribute), true) .Any() && !i.IsAbstract && !i.IsInterface && (typeof(ITarget).IsAssignableFrom(i) || typeof(IMapperTarget).IsAssignableFrom(i)) && i.GetInterfaces() .Any(g => g.IsGenericType && g.GetType().GetInterfaces().Any())) .Select(map => new MapTarget { target = map , mapName = map.Name , baseType = map.BaseType , source = GetSource <ITarget>(map) , inheritOrder = GetTypeInheritsNumber(map) } ); //var Types = maps // .Where(w => w.inheritOrder == // maps // .Where(w2 => w2.source.Name == w.source.Name) // .Max(m => m.inheritOrder)); //var haveDups = Types // .Select(s => s) // .GroupBy(g => new { g.target.Name // ,g.inheritOrder} // ) // .Where(w => w.Skip(1).Any()) // .SelectMany(sm => sm); //if (haveDups != null && haveDups.Any()) //{ // //throw // // new Exception( // // string.Format("Found Multiple Mapping for {0}", haveDups.FirstOrDefault().Name) // // ); //} var cfg = new MapperConfigurationExpression(); if (_profile != null) { cfg.AddProfile(_profile); } if (cfg.Profiles.Count() == 0) { cfg.AddProfile(new DefaultProfile <IComplexType, ITarget>()); } var custom = cfg .Profiles.Select(s => s.TypeMapConfigs) .SelectMany(sm => sm) .Select(s => s); cfg.AddProfile(new UpdateEntitiesProfile <IComplexType, ITarget>(Types)); foreach (var map in Types .Where(w => !custom.Any(ww => ww.SourceType.Equals(w.source))) .OrderByDescending(o => o.inheritOrder)) // sort by inheritance { // inherited view model objects // must use specific type // calls to mapper if (!IscomplexMapping <IComplexType>(map)) { if (!typeof(ITarget).IsAssignableFrom(map.target)) { cfg.CreateMap(map.source, map.target) .ReverseMap(); } else { cfg.CreateMap(map.source, map.target) .ReverseMap(); } } else { cfg.CreateMap(map.source, map.target, MemberList.None) .ProjectUsing(src => MapSetter.ModelDestination <ITarget, IComplexType>(src, map.target)); cfg.CreateMap(map.target, map.source) .ProjectUsing(src => MapSetter.EntityDestination <ITarget, IComplexType>(src, map.source)); } } _mapper = new MapperConfiguration(cfg).CreateMapper(); _mapper.ConfigurationProvider.CompileMappings(); }