|
| 1 | +Multiple, dedicated configuration classes can be created by deriving from the abstract `MapperConfiguration` base class. This enables configuration to be split up and placed nearer where mapping is performed (although [inline](Inline-Configuration) is nearer). |
| 2 | + |
| 3 | +`MapperConfiguration` provides the same configuration API, and exposes services you inject. |
| 4 | + |
| 5 | +### Configuration and Caching |
| 6 | + |
| 7 | +Mapper configuration is set up by implementing the abstract `Configure` method: |
| 8 | + |
| 9 | +```C# |
| 10 | +public class ProductMappingConfiguration : MapperConfiguration |
| 11 | +{ |
| 12 | + protected override void Configure() |
| 13 | + { |
| 14 | + // Configure default Mapper ProductDto -> Productmapping: |
| 15 | + WhenMapping |
| 16 | + .From<ProductDto>() |
| 17 | + .To<Product>() |
| 18 | + .Map((dto, p) => dto.Spec) |
| 19 | + .To(p => p.Specification) |
| 20 | + .And |
| 21 | + .Ignore(p => p.Price, p => p.CatNum); |
| 22 | + |
| 23 | + // Cache all Product -> ProductDto mapping plans: |
| 24 | + GetPlansFor<Product>().To<ProductDto>(); |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +In this example the default mapper is configured - the one used via the [static](Static-vs-Instance-Mappers) Mapper API. |
| 30 | + |
| 31 | +### Applying Configurations |
| 32 | + |
| 33 | +`MapperConfiguration` classes can be discovered and applied in various ways. |
| 34 | + |
| 35 | +To apply a particular `MapperConfiguration` Type, supply it explicitly: |
| 36 | + |
| 37 | +```C# |
| 38 | +Mapper.WhenMapping |
| 39 | + .UseConfigurations.From<ProductMappingConfiguration>(); |
| 40 | +``` |
| 41 | + |
| 42 | +To apply all `MapperConfiguration` Types from an Assembly, supply a Type from that Assembly: |
| 43 | + |
| 44 | +```C# |
| 45 | +Mapper.WhenMapping |
| 46 | + .UseConfigurations.FromAssemblyOf<Product>(); |
| 47 | +``` |
| 48 | + |
| 49 | +To apply all `MapperConfiguration` Types from multiple Assemblies, supply the Assemblies: |
| 50 | + |
| 51 | +```C# |
| 52 | +// Scan all Assemblies from the AppDomain: |
| 53 | +Mapper.WhenMapping |
| 54 | + .UseConfigurations.From(assembly1, assembly2, assembly3); |
| 55 | + |
| 56 | +// Scan all the given Assemblies which match the filter - |
| 57 | +// Assembly scanning can be expensive, so this can be useful! |
| 58 | +Mapper.WhenMapping |
| 59 | + .UseConfigurations.From( |
| 60 | + GetLotsOfAssemblies(), |
| 61 | + assembly => assembly.FullName.Contains(nameof(MyNamespace))); |
| 62 | +``` |
| 63 | + |
| 64 | +To apply all `MapperConfiguration` Types from the Assemblies current loaded into the `AppDomain`, use: |
| 65 | + |
| 66 | +```C# |
| 67 | +// Scan all Assemblies from the AppDomain: |
| 68 | +Mapper.WhenMapping |
| 69 | + .UseConfigurations.FromCurrentAppDomain(); |
| 70 | + |
| 71 | +// Scan all Assemblies from the AppDomain which match the filter - |
| 72 | +// Assembly scanning can be expensive, so this is advisable! |
| 73 | +Mapper.WhenMapping |
| 74 | + .UseConfigurations.FromCurrentAppDomain( |
| 75 | + assembly => assembly.FullName.Contains("MyCompanyName")); |
| 76 | +``` |
| 77 | + |
| 78 | +### Ordering Configurations |
| 79 | + |
| 80 | +Calling `GetPlansFor<Source>().To<Target>()` caches the mapping function at the point you call it. If Types configured in the object graph are configured in more than one `MapperConfiguration`, you might need to define an order in which configuration classes are applied. Use: |
| 81 | + |
| 82 | +```C# |
| 83 | +// Configure aspects of Parent -> Parent mapping, which includes |
| 84 | +// mapping Child -> Child. Automatically apply ChildMapperConfiguration, |
| 85 | +// then apply this configuration afterwards. |
| 86 | +[ApplyAfter(typeof(ChildMapperConfiguration))] |
| 87 | +public class ParentMapperConfiguration : MapperConfiguration |
| 88 | +{ |
| 89 | +} |
| 90 | + |
| 91 | +// Configure aspects of Child -> Child mapping: |
| 92 | +public class ChildMapperConfiguration : MapperConfiguration |
| 93 | +{ |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +Chains of `ApplyAfter` attributes will be followed, with all configurations automatically applied in the defined order. Defining circular references between configuration types will throw a `MappingConfigurationException`. |
| 98 | + |
| 99 | +### Accessing Services |
| 100 | + |
| 101 | +[Configured Service Providers](Dependency-Injection) are available to `MapperConfiguration` classes. For example: |
| 102 | + |
| 103 | +```C# |
| 104 | +// Get a Dependency Injection container: |
| 105 | +var diContainer = GetDiContainer(); |
| 106 | + |
| 107 | +// Register the container: |
| 108 | +Mapper.WhenMapping.UseServiceProvider(diContainer); |
| 109 | + |
| 110 | +// Scan for MapperConfigurations: |
| 111 | +Mapper.WhenMapping |
| 112 | + .UseConfigurations.FromAssemblyOf<Product>(); |
| 113 | +``` |
| 114 | + |
| 115 | +...the DI container and its registered services are now available to the `MapperConfiguration` class via the `GetService<TService>()` and `GetServiceProvider<TContainer>()` methods: |
| 116 | + |
| 117 | +```C# |
| 118 | +public class MyMappingConfiguration : MapperConfiguration |
| 119 | +{ |
| 120 | + protected override void Configure() |
| 121 | + { |
| 122 | + // Get a reference to the configured container: |
| 123 | + var diContainer = GetServiceProvider<IUnityContainer>(); |
| 124 | + |
| 125 | + // Get a reference to a configured ILogger - this just passes |
| 126 | + // the request to the container and returns the result: |
| 127 | + var logger = GetService<ILogger>(); |
| 128 | + |
| 129 | + // Create a new mapper for Product mapping: |
| 130 | + var productMapper = CreateNewMapper(); |
| 131 | + |
| 132 | + // Configure productMapper Product -> ProductDto mapping: |
| 133 | + productMapper.WhenMapping |
| 134 | + .From<ProductDto>() |
| 135 | + .To<Product>() |
| 136 | + .Map((dto, p) => dto.Spec) |
| 137 | + .To(p => p.Specification); |
| 138 | + |
| 139 | + // Cache all Product -> ProductDto mapping plans: |
| 140 | + productMapper.GetPlansFor<Product>().To<ProductDto>(); |
| 141 | + |
| 142 | + // Create a new mapper for Order mapping: |
| 143 | + var orderMapper = CreateNewMapper(); |
| 144 | + |
| 145 | + // Configure orderMapper Order -> OrderDto create new mapping: |
| 146 | + orderMapper.WhenMapping |
| 147 | + .From<Order>() |
| 148 | + .ToANew<OrderDto>() |
| 149 | + .Map((o, dto) => o.Items.Sum(i => i.Cost)) |
| 150 | + .To(dto => dto.TotalCost); |
| 151 | + |
| 152 | + // Cache the Order -> OrderDto create new mapping plan: |
| 153 | + orderMapper.GetPlanFor<Order>().ToANew<OrderDto>(); |
| 154 | + |
| 155 | + // Register named IMapper instances with the container: |
| 156 | + diContainer.RegisterInstance("ProductMapper", productMapper); |
| 157 | + diContainer.RegisterInstance("OrderMapper", orderMapper); |
| 158 | + |
| 159 | + logger.LogDebug("Product and Order mapping configured and registered"); |
| 160 | + } |
| 161 | +} |
| 162 | +``` |
0 commit comments