# EggMapper > Fast .NET object mapper with AutoMapper's API (CreateMap, ForMember, Profile, IMapper). Maps compile to expression trees on first use. MIT OR Apache-2.0. `dotnet add package EggMapper` (DI included). ## Quick start ```csharp compile var config = new MapperConfiguration(cfg => { cfg.CreateMap() .ForMember(d => d.CustomerName, o => o.MapFrom(s => s.Customer.Name)); cfg.CreateMap(); }); IMapper mapper = config.CreateMapper(); OrderDto dto = mapper.Map(order); ``` DI: `services.AddEggMapper(typeof(OrderProfile).Assembly)` registers `MapperConfiguration` (singleton), `IMapper` and `IMapper` (transient). ## API card | Call | Use | |---|---| | `mapper.Map(src)` | Preferred: typed, cached, fastest | | `mapper.Map(obj)` | Runtime source type (AutoMapper style) | | `mapper.Map(src, existing)` | Map into an existing object | | `mapper.MapList(items)` | List mapping, one compiled loop | | `mapper.Patch(src, existing)` | Copy same-name members whose source value is not null (needs `CreateMap`) | | `IMapper.Map(src)` | Inject on hot paths (no generic virtual call) | | `query.ProjectTo(config)` | IQueryable/EF projection; BOTH type args | | `config.CompileMappings()` | Compile all maps now | | `config.AssertConfigurationIsValid()` | Fail on unmapped destination members | ## Configuration ```csharp compile public class OrderProfile : Profile { public OrderProfile() { CreateMap() .ForMember(d => d.Total, o => o.MapFrom(s => s.Items.Sum(i => i.Price))) .ForMember(d => d.Internal, o => o.Ignore()) .ForMember(d => d.Note, o => o.NullSubstitute("n/a")) .ForMember(d => d.CustomerName, o => o.Condition(s => s.Customer != null)) .Validate(d => d.Total, total => total >= 0, "Total must not be negative"); CreateMap().ReverseMap(); } } ``` Also: `ForPath` (top-level members only), `IncludeBase()`, `MaxDepth(n)`, `ConvertUsing(s => ...)`, `ConstructUsing`, `BeforeMap`/`AfterMap`, `AddTypeConverter(...)`, `cfg.AddMaps(assembly)`, `cfg.ShouldMapProperty`. Conventions: same names (case-insensitive), flattening (`Customer.Name` -> `CustomerName`), records/constructors, collections and nested maps, `Map` without `CreateMap` (shallow copy; classes with a parameterless constructor), enums by name (`cfg.EnumMapping`). ## Validation (add once) `dotnet add package EggMapper.Analyzers`: each unmapped destination member of a `CreateMap` is a build warning (EGG1004) whose message holds the `ForMember` fix. Plus one test: ```csharp compile var config = new MapperConfiguration(cfg => cfg.AddMaps(typeof(OrderProfile).Assembly)); config.CompileMappings(); config.AssertConfigurationIsValid(); ``` ## From AutoMapper Change the package, `using AutoMapper` -> `using EggMapper`, `AddAutoMapper(typeof(X))` -> `AddEggMapper(typeof(X).Assembly)`. Differences: | AutoMapper | EggMapper | |---|---| | `ProjectTo(cfg)` | `ProjectTo(cfg)` | | `mapper.ConfigurationProvider` | keep the `MapperConfiguration` instance | | `ForCtorParam`, `ForSourceMember`, `IncludeMembers`, `Include<,>`/`As<>` (polymorphic) | not supported; use `ForMember`/`ConstructUsing`, map derived types explicitly | | `IValueResolver` | `IMemberValueResolver` via `MapFrom(s => ...)`; needs the DI mapper | | `ForPath(d => d.A.B, ...)` | not supported; give the nested object its own `CreateMap` | | `ReverseMap()` inverting `MapFrom`/unflattening | `ReverseMap()` maps by convention only; configure the reverse with its own `CreateMap` | `IncludeAllDerived()` shares base configuration with derived maps; it does not pick a derived map at run time. `ProjectTo` ignores child collections, resolvers, `Condition`, `NullSubstitute` and nested `ConvertUsing`. ## Errors -> fix | Message | Fix | |---|---| | `No mapping configured for S -> D` | `cfg.CreateMap()` (or `.ReverseMap()` on `CreateMap()`) | | `Unmapped destination member 'D.X'` | paste the message's `Fix:` line; else pick from `Options:` with `.ForMember(d => d.X, o => o.MapFrom(s => ...))` or `o.Ignore()` | | `MappingValidationException` | a `Validate` rule failed; message lists each rule | | `Circular IncludeBase()` | remove the cycle between `IncludeBase` calls | ## Docs - [Full reference](https://eggspot.github.io/EggMapper/llms-full.txt) - [Getting started](https://eggspot.github.io/EggMapper/Getting-Started.html) - [Configuration](https://eggspot.github.io/EggMapper/Configuration.html) - [Migration from AutoMapper](https://eggspot.github.io/EggMapper/Migration-Guide.html) - [API reference](https://eggspot.github.io/EggMapper/API-Reference.html)