# EggMapper — full reference for AI coding assistants Fast .NET object mapper with AutoMapper's API. Package `EggMapper` (runtime, includes DI). Compile-time tiers: `EggMapper.Generator` (`[MapTo]`) and `EggMapper.ClassMapper` (`[EggMapper]` partial classes). Targets netstandard2.0, net462, net8.0-net11.0. License MIT OR Apache-2.0. Short version: llms.txt. ## Rules that save round trips - Register maps in `Profile` classes; scan with `cfg.AddMaps(assembly)` or `services.AddEggMapper(assembly)`. - Prefer `mapper.Map(src)`; inject `IMapper` on hot paths. - Keep one `MapperConfiguration` (singleton). Maps compile on first use; `CompileMappings()` compiles all. - Add one test calling `CompileMappings()` and `AssertConfigurationIsValid()`. - `ProjectTo(config)` needs both type arguments. - Read error messages: they name the missing `CreateMap` or member. - Reference `EggMapper.Analyzers`: EGG1004 turns each unmapped destination member into a build warning with the fix. ## Configuration and mapping ```csharp compile public class Address { public string Street { get; set; } = ""; public string City { get; set; } = ""; } public class Person { public int Id { get; set; } public string FirstName { get; set; } = ""; public string LastName { get; set; } = ""; public Address Address { get; set; } = new(); public string? Nickname { get; set; } } public class PersonDto { public int Id { get; set; } public string FullName { get; set; } = ""; public string AddressCity { get; set; } = ""; public string Nickname { get; set; } = ""; public string Secret { get; set; } = ""; } public record PersonSummary(int Id, string FirstName); public class PersonUpdate { public string? FirstName { get; set; } public string? LastName { get; set; } public string? Nickname { get; set; } } public class PersonProfile : Profile { public PersonProfile() { CreateMap() .ForMember(d => d.FullName, o => o.MapFrom(s => s.FirstName + " " + s.LastName)) .ForMember(d => d.Nickname, o => o.NullSubstitute("none")) .ForMember(d => d.Secret, o => o.Ignore()); CreateMap(); CreateMap() .ForMember(d => d.Id, o => o.Ignore()) .ForMember(d => d.Address, o => o.Ignore()); } } ``` `AddressCity` maps from `Address.City` by flattening. `PersonSummary` is built through its constructor. ```csharp compile var config = new MapperConfiguration(cfg => cfg.AddMaps(typeof(PersonProfile).Assembly)); var mapper = config.CreateMapper(); var person = new Person { Id = 1, FirstName = "Ada", LastName = "Lovelace" }; PersonDto dto = mapper.Map(person); PersonDto viaObject = mapper.Map(person); mapper.Map(person, dto); List many = mapper.MapList(new List { person }); mapper.Patch(new PersonUpdate { Nickname = "Ace" }, person); Person copy = mapper.Map(person); IMapper typed = config.CreateMapper(); PersonDto fast = typed.Map(person); ``` `Patch` needs a `CreateMap` and copies same-name members (and `MapFrom("Name")`) whose source value is not null; give the patch type nullable members, because non-nullable value types are always copied. It skips expression `MapFrom`, `Condition`, `NullSubstitute`, resolvers, flattened members, collections and nested objects of a different type. A non-null nested object of the same type is assigned as is (the destination then holds the patch's instance). `Map` works without a `CreateMap` for classes with a parameterless constructor (records and other constructor-only types need `CreateMap()`). The copy is shallow: nested objects keep their references, and collections become new collections holding the same elements. ## Member options `ForMember(d => d.X, o => ...)`: `MapFrom(s => expr)`, `MapFrom("SourceName")`, `Ignore()`, `Condition(s => bool)`, `PreCondition(s => bool)`, `NullSubstitute(value)`, `UseValue(value)`, `UseDestinationValue()`, `MapFrom(s => s.Member)` (resolver below). `ForPath(d => d.X, ...)` accepts only a top-level destination member; nested paths (`d => d.A.B`) are not supported (they throw, or set a top-level member with the same name). `ForAllOtherMembers(o => o.Ignore())` ignores everything not configured. ```csharp compile public class CustomerDto { public string Name { get; set; } = ""; } public class UpperCaseResolver : IMemberValueResolver { public string Resolve(object source, object destination, string sourceMember, string destMember, ResolutionContext context) => sourceMember.ToUpperInvariant(); } public class CustomerProfile : Profile { public CustomerProfile() { CreateMap() .ForMember(d => d.Name, o => o.MapFrom(s => s.Name)); } } ``` Resolvers implement `IMemberValueResolver` (source and destination arrive as `object`). They need a mapper from DI (`services.AddEggMapper`); constructor dependencies come from the container, and the resolver itself does not have to be registered. ## Map-level options ```csharp compile var config = new MapperConfiguration(cfg => { cfg.CreateMap() .BeforeMap((s, d) => d.Note = "") .AfterMap((s, d) => d.Total = s.Items.Sum(i => i.Price)) .ForMember(d => d.CustomerName, o => o.MapFrom(s => s.Customer!.Name)) .ForMember(d => d.Internal, o => o.Ignore()) .Validate(d => d.Total, total => total >= 0, "Total must not be negative"); cfg.CreateMap().ReverseMap(); cfg.CreateMap().ConvertUsing(p => new ProductDto { Id = p.Id }); cfg.AddTypeConverter(d => d.ToString("O")); cfg.ShouldMapProperty = p => p.Name != "Internal"; }); config.CompileMappings(); ``` Also: `ConstructUsing(s => new D(...))`, `IncludeBase()` (inherit a base map's configuration), `MaxDepth(n)` (self-referencing types), `CreateMap(typeof(Page<>), typeof(PageDto<>))` (open generics), `ConvertUsing()` with `ITypeConverter`, call-site options `mapper.Map(src, opts => opts.AfterMap((s, d) => ...))` with `opts.Items`. ## Enums Different enum types map by name (exact, then ignoring case, else by value), like AutoMapper. `cfg.EnumMapping = EnumMappingMode.ByValue` restores numeric mapping. Per pair: `CreateMap().ConvertUsingEnumMapping(o => o.MapByName().MapValue(EA.X, EB.Y))`. Enum <-> string and int <-> enum convert automatically. `ProjectTo` maps enums by value. ## Dependency injection ```csharp compile services.AddEggMapper(typeof(OrderItemDto).Assembly); services.AddEggMapper(cfg => cfg.CreateMap()); ``` Registers `MapperConfiguration` (singleton), `IMapper` and `IMapper` (transient; each carries the scope's `IServiceProvider`, so DI value resolvers get scoped services). ## IQueryable projection ```csharp compile var config = new MapperConfiguration(cfg => cfg.CreateMap()); IQueryable query = products.ProjectTo(config); ``` Translated: same-name, flattened, nested registered maps (by convention), `MapFrom` expressions, enums by value. Ignored: child collections, resolvers, `Condition`, `NullSubstitute`, and `ConvertUsing` on nested maps. ## Compile-time tiers (separate packages) ```csharp needs-generator [MapTo(typeof(OrderDto))] // EggMapper.Generator: generates order.ToOrderDto() public class Order { /* ... */ } [EggMapper] // EggMapper.ClassMapper: generates the method bodies public partial class OrderMapper { public partial OrderDto Map(Order source); } ``` ## Differences from AutoMapper | AutoMapper | EggMapper | |---|---| | `ProjectTo(cfg)` | `ProjectTo(cfg)` | | `services.AddAutoMapper(typeof(X))` | `services.AddEggMapper(typeof(X).Assembly)` | | `mapper.ConfigurationProvider` | keep the `MapperConfiguration` instance | | `ForPath(d => d.A.B, ...)` | not supported; map the nested object with its own `CreateMap` | | `ForCtorParam` | constructor parameters match source members by name; else `ConstructUsing` | | `ForSourceMember`, `IncludeMembers` | not supported; use `ForMember` with `MapFrom` | | `Include<,>`, `As<>`, polymorphic dispatch | not supported; `IncludeAllDerived()` only shares base configuration | | `IValueResolver` | `IMemberValueResolver` with `MapFrom` | | `ReverseMap()` inverting `MapFrom` / unflattening | convention-only reverse map; configure it with its own `CreateMap` | | AutoMapper licence (RPL-1.5 or commercial from v15) | MIT OR Apache-2.0 | ## Errors and fixes | Message | Fix | |---|---| | `No mapping configured for S -> D` | `cfg.CreateMap()`; for a list, the element map; the message names `.ReverseMap()` when `CreateMap()` exists | | `EGG1004: Destination member 'D.X' has no source member in CreateMap()` | paste the `Fix:` from the warning; else `ForMember(d => d.X, o => o.MapFrom(s => ...))` or `o.Ignore()` | | `Unmapped destination member 'D.X' in map S -> D` | paste the message's `Fix:` line (given for a confident match); else pick a source member from `Options:` or `o.Ignore()` | | `MappingValidationException` | a `Validate` rule failed; the message lists every failed rule | | `Cannot convert String value 'abc' to Int32` | the source value has no conversion: `ForMember(d => d.X, o => o.MapFrom(s => ...))` or `cfg.AddTypeConverter(...)` | | `MappingException` wrapping another error | the inner exception names the cause; check `MapFrom` expressions | | `Circular IncludeBase() reference` | remove the cycle between `IncludeBase` calls | | `CreateMap() is configured in P1 and P2` | keep one map for the pair; move the other's `ForMember` calls into it | | `IMemberValueResolver requires DI` | map with the `IMapper` from `services.AddEggMapper(...)`, not `config.CreateMapper()` | | `Property 'X' not found or not writable on 'D'` | `ForPath` got a nested path (configure the nested map instead) or a read-only member | ## Performance Benchmarks (BenchmarkDotNet, .NET 10) are in the README and regenerated by CI on every merge: https://github.com/eggspot/EggMapper#performance