### Mapper Cloning Example Source: https://github.com/agileobjects/agilemapper/blob/master/docs/src/configuration/Cloning-Mappers.md Demonstrates cloning a base mapper and applying specific configurations for UK and US orders. ```APIDOC ## Mapper Cloning and Derived Mappers ### Description This example illustrates how to clone a base mapper configuration to create specialized mappers for different regions (UK and US). The derived mappers inherit the base configuration and add their own specific mappings. ### Method N/A (This is a code example, not an API endpoint) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ## Code Example (C#) ```cs // Create a base mapper var baseMapper = Mapper.CreateNew(); // Setup the base configuration for Order to OrderDto baseMapper.WhenMapping .From() .To() .Map((o, dto) => o.ProdId) .To(dto => dto.ProductId) .And .Map((o, dto) => o.Id) .To(dto => dto.OrderNumber); // Clone a new mapper for UK orders var ukOrderMapper = baseMapper.CloneSelf(); // Setup UK-specific configuration: add a specific DateCreated mapping ukOrderMapper.WhenMapping .To() .Map(ctx => DateTime.UtcNow.AddHours(1)) .To(dto => dto.DateCreated); // Clone a new mapper for US orders var usOrderMapper = baseMapper.CloneSelf(); // Setup US-specific configuration: add a specific DateCreated mapping usOrderMapper.WhenMapping .To() .Map(ctx => DateTime.UtcNow.AddHours(-4)) .To(dto => dto.DateCreated); ``` ``` -------------------------------- ### Install AgileMapper via NuGet Source: https://context7.com/agileobjects/agilemapper/llms.txt Command to install the AgileMapper package using the NuGet Package Manager console. ```bash PM> Install-Package AgileObjects.AgileMapper ``` -------------------------------- ### Configure Custom Object Construction Source: https://context7.com/agileobjects/agilemapper/llms.txt Provides examples of defining custom factories for object creation. This includes global configurations, type-pair specific factories, and conditional logic during the mapping process. ```csharp Mapper.WhenMapping.InstancesOf().CreateUsing(ctx => new Customer { Number = ctx.EnumerableIndex }); Mapper.WhenMapping.From().To().CreateInstancesUsing((pvm, c, index) => new Customer { Number = index }); Mapper.Map(customerViewModels).ToANew(cfg => cfg.WhenMapping.From().To().If((cvm, c) => cvm.Discount > 0).CreateInstancesUsing((cvm, c, i) => new Customer { HasDiscount = true, Number = i })); ``` -------------------------------- ### Viewing Mapping Execution Plans with AgileMapper Source: https://github.com/agileobjects/agilemapper/blob/master/docs/src/index.md Provides examples of how to retrieve and view the execution plan for various mapping operations (creation, updates, merges, query projection) using AgileMapper. This also caches the plan. ```csharp // For object creation: string mappingPlan = Mapper.GetPlanFor().ToANew(); // For updates: string mappingPlan = Mapper.GetPlanFor().Over(); // For merges: string mappingPlan = Mapper.GetPlanFor().OnTo(); // For all three in one call: string mappingPlans = Mapper.GetPlansFor().To(); // For query projection: string mappingPlan = Mapper .GetPlanForProjecting(context.Customers) .To(); // For everything in the mapping cache: var allPlans = Mapper.GetPlansInCache(); ``` -------------------------------- ### Apply Multiple Inline Mapping Rules Source: https://github.com/agileobjects/agilemapper/blob/master/docs/src/configuration/Inline.md Provides examples of chaining multiple configuration rules using the .And operator or by passing multiple configuration delegates. ```csharp var dto = mapper .Map(product).ToANew(cfg => cfg .Map((p, d) => p.Spec).To(d => d.Specification) .And .Map((p, d) => p.Price).To(d => d.Cost)); // Or if you prefer: var dto = mapper .Map(product).ToANew( cfg => cfg.Map((p, d) => p.Spec).To(d => d.Specification), cfg => cfg.Map((p, d) => p.Price).To(d => d.Cost)); ``` -------------------------------- ### Configuring Mappers and Accessing Services in MapperConfiguration Source: https://github.com/agileobjects/agilemapper/blob/master/docs/src/configuration/Classes.md Shows a detailed example of a MapperConfiguration class that retrieves a DI container, accesses services like ILogger, creates and configures mappers for Product and Order objects, and registers these mappers back into the DI container. ```csharp public class MyMappingConfiguration : MapperConfiguration { protected override void Configure() { // Get a reference to the configured container: var diContainer = GetServiceProvider(); // Get a reference to a configured ILogger - this just passes // the request to the container and returns the result: var logger = GetService(); // Create a new mapper for Product mapping: var productMapper = CreateNewMapper(); // Configure productMapper Product -> ProductDto mapping: productMapper.WhenMapping .From() .To() .Map((dto, p) => dto.Spec) .To(p => p.Specification); // Cache all Product -> ProductDto mapping plans: productMapper.GetPlansFor().To(); // Create a new mapper for Order mapping: var orderMapper = CreateNewMapper(); // Configure orderMapper Order -> OrderDto create new mapping: orderMapper.WhenMapping .From() .ToANew() .Map((o, dto) => o.Items.Sum(i => i.Cost)) .To(dto => dto.TotalCost); // Cache the Order -> OrderDto create new mapping plan: orderMapper.GetPlanFor().ToANew(); // Register named IMapper instances with the container: diContainer.RegisterInstance("ProductMapper", productMapper); diContainer.RegisterInstance("OrderMapper", orderMapper); logger.LogDebug("Product and Order mapping configured and registered"); } } ``` -------------------------------- ### Switching Data Source in Agile Mapper Source: https://github.com/agileobjects/agilemapper/blob/master/docs/src/configuration/Member-Values.md Shows how to redirect a mapping to use a different data source value. The example demonstrates mapping a dictionary's values to a list of DTOs, using `ToTargetInstead()` to replace the default mapping behavior. ```csharp class VideoLibrary { public Dictionary VideosById { get; set; } } class VideoLibraryDto { public IList Videos { get; set; } } Mapper.WhenMapping .FromDictionariesWithValueType