private void AddMapsCore(IEnumerable <Assembly> assembliesToScan) { var allTypes = assembliesToScan.Where(a => !a.IsDynamic && a != typeof(NamedProfile).Assembly).SelectMany(a => a.GetDefinedTypes()).ToArray(); var autoMapAttributeProfile = new NamedProfile(nameof(AutoMapAttribute)); foreach (var type in allTypes) { if (typeof(Profile).IsAssignableFrom(type) && !type.IsAbstract) { AddProfile(type.AsType()); } foreach (var autoMapAttribute in type.GetCustomAttributes <AutoMapAttribute>()) { var mappingExpression = (MappingExpression)autoMapAttributeProfile.CreateMap(autoMapAttribute.SourceType, type); autoMapAttribute.ApplyConfiguration(mappingExpression); foreach (var memberInfo in type.GetMembers(BindingFlags.Public | BindingFlags.Instance)) { foreach (var memberConfigurationProvider in memberInfo.GetCustomAttributes().OfType <IMemberConfigurationProvider>()) { mappingExpression.ForMember(memberInfo, cfg => memberConfigurationProvider.ApplyConfiguration(cfg)); } } } } AddProfile(autoMapAttributeProfile); }
private void AddAssembliesCore(RelatedDtoLoaderAssemblyOptions options, IEnumerable <Assembly> assembliesToScan) { options = options ?? new RelatedDtoLoaderAssemblyOptions(); var allTypes = assembliesToScan.Where(a => !a.IsDynamic && a != typeof(NamedProfile).Assembly) .SelectMany(a => a.DefinedTypes) .Where(x => !x.IsAbstract) .ToArray(); var profileTypes = allTypes.Where(x => ProfileType.IsAssignableFrom(x)).ToArray(); foreach (var type in profileTypes) { var profile = (IRelatedDtoLoaderProfile)Activator.CreateInstance(type); AddProfile(profile); } var dynamicLoaderProfile = new NamedProfile(); if (options.AutoEnableTargetDtoTypes) { var dtoTypes = allTypes.Where(x => EntityDtoType.IsAssignableFrom(x)).ToArray(); foreach (var type in dtoTypes) { if (EntityDtoType.IsAssignableFrom(type)) { dynamicLoaderProfile.LoadForDto(type); } } } AddProfile(dynamicLoaderProfile); }
public void CreateProfile(string profileName, Action <Profile> config) { var profile = new NamedProfile(profileName); config(profile); AddProfile(profile); }
void IConfiguration.CreateProfile(string profileName, Action <Profile> config) { var profile = new NamedProfile(profileName); config(profile); ((IConfiguration)this).AddProfile(profile); }
private Profile CreateProfile(string profileName) { var profileExpression = new NamedProfile(profileName); profileExpression.Initialize(this); _profiles.AddOrUpdate(profileExpression.ProfileName, profileExpression, (s, configuration) => profileExpression); return(profileExpression); }
public static void AddProfilesForAttributes(this MapperConfigurationExpression cfg, IEnumerable <Assembly> assembliesToScan, AddedMapContext mapContext, IFactory factory) { var allTypes = assembliesToScan.Where(a => !a.IsDynamic && a != typeof(NamedProfile).Assembly).SelectMany(a => a.DefinedTypes).ToArray(); var autoMapAttributeProfile = new NamedProfile(nameof(YuzuMapAttribute)); var config = factory.GetInstance <IYuzuConfiguration>(); var importConfig = factory.GetInstance <IYuzuDeliveryImportConfiguration>(); foreach (var viewModels in allTypes) { foreach (var attribute in viewModels.GetCustomAttributes <YuzuMapAttribute>()) { var cmsModel = config.CMSModels.Where(x => x.Name == attribute.SourceTypeName).FirstOrDefault(); if (cmsModel != null && !mapContext.Has(cmsModel, viewModels) && !importConfig.IgnoreUmbracoModelsForAutomap.Contains(cmsModel.Name)) { cfg.CreateMap(cmsModel, viewModels); } } } }
//taken from https://github.com/AutoMapper/AutoMapper/blob/master/src/AutoMapper/Configuration/MapperConfigurationExpression.cs private Profile HandleAutoMapTypes(params Type[] types) { var autoMapAttributeProfile = new NamedProfile(nameof(AutoMapAttribute)); foreach (var type in types) { foreach (var autoMapAttribute in type.GetCustomAttributes <AutoMapAttribute>()) { var mappingExpression = (MappingExpression)autoMapAttributeProfile.CreateMap(autoMapAttribute.SourceType, type); autoMapAttribute.ApplyConfiguration(mappingExpression); foreach (var memberInfo in type.GetMembers(BindingFlags.Public | BindingFlags.Instance)) { foreach (var memberConfigurationProvider in memberInfo.GetCustomAttributes().OfType <IMemberConfigurationProvider>()) { mappingExpression.ForMember(memberInfo.Name, cfg => memberConfigurationProvider.ApplyConfiguration(cfg)); } } } } return(autoMapAttributeProfile); }