private void CreateAttributeBuilders() { foreach (var assembly in Config.AttributeAssemblies) { foreach (var type in assembly.GetTypes()) { var attribute = type.GetCustomAttributes(typeof(MapFromAttribute), false).FirstOrDefault(); if (attribute != null) { var sourceType = (attribute as MapFromAttribute).SourceType; var key = new MappingKey(sourceType, type); if (Config.Builders.ContainsKey(key)) { throw new MappingAlreadyExistsException("Another mapping already exists with the supplied types"); } var builder = CreateMappingBuilder(sourceType, type); Config.Builders.Add(key, builder); Config.BuilderDescriptors.Add(new BuilderDescriptor(builder.SourceType, builder.TargetType, builder.Dependencies, true)); } } } }
public void CreateMapping <TSource, TTarget>() { var key = new MappingKey(typeof(TSource), typeof(TTarget)); if (Builders.ContainsKey(key)) { throw new MappingAlreadyExistsException("Another mapping already exists with the supplied types"); } var builder = new MappingBuilder <TSource, TTarget>(this); builder.CreateDefaultBindings(); Builders.Add(key, builder); BuilderDescriptors.Add(new BuilderDescriptor(typeof(TSource), typeof(TTarget), builder.Dependencies, true)); }
/// <summary> /// Creates a new mapper object using the registered mappings or the default mappings where necessary. /// </summary> /// <returns>Returns a new <see cref="IMapper"/> object that contains the mappings.</returns> public IMapper CreateMapper() { CreateAttributeBuilders(); var comparer = EqualityComparer <BuilderDescriptor> .Default; // Check for circular dependency Config.BuilderDescriptors.TopologicalSort(b => b.Dependencies, comparer); var mappings = new Dictionary <MappingKey, IMapping>(); // Create all non-user defined mappings CreateDefaultBuilders(); foreach (var builderDescriptor in Config.BuilderDescriptors.TopologicalSort(b => b.Dependencies, comparer)) { var key = new MappingKey(builderDescriptor.SourceType, builderDescriptor.TargetType); Config.Builders.TryGetValue(key, out var builder); var dependencies = mappings.Where(m => builder.Dependencies.Any(d => d.SourceType == m.Key.SourceType && d.TargetType == m.Key.TargetType)).ToDictionary(i => i.Key, i => i.Value); var mapping = builder.Build(dependencies); mappings.Add(key, mapping); } foreach (var parametrizedBuilder in Config.ParametrizedBuilders) { var dependencies = mappings.Where(m => parametrizedBuilder.Value.Builder.Dependencies.Any(d => d.SourceType == m.Key.SourceType && d.TargetType == m.Key.TargetType)).ToDictionary(i => i.Key, i => i.Value); // This just stores the dependencies for later use parametrizedBuilder.Value.Builder.Build(dependencies); mappings.Add(parametrizedBuilder.Key, parametrizedBuilder.Value.Mapping); } return(new Mapper { Mappings = mappings }); }
public void CreateMapping <TSource, TTarget, TParam>(Action <IMappingBuilder <TSource, TTarget, TParam> > mappingFactory) { var key = new MappingKey(typeof(TSource), typeof(TTarget), typeof(TParam)); if (ParametrizedBuilders.ContainsKey(key)) { throw new MappingAlreadyExistsException("Another mapping already exists with the supplied types"); } var builder = new MappingBuilder <TSource, TTarget, TParam>(this); mappingFactory(builder); builder.CreateDefaultBindings(); // Should be run after the explicit mappings ParametrizedBuilders.Add(key, new ParametrizedBuilderDescriptor { Mapping = new Mapping <TSource, TTarget, TParam> { Builder = builder }, Builder = builder }); }