ZeroAlloc.Mapping is a Roslyn source generator that emits strongly-typed Command→Domain→DTO mappers at compile time. No reflection. No expression trees. No runtime configuration. The success path runs with zero allocation beyond the destination instance itself, and the generated code is fully Native AOT compatible.
dotnet add package ZeroAlloc.MappingThe generator runs automatically as part of dotnet build — there is no DI registration or runtime configuration.
using ZeroAlloc.Mapping;
// 1. Define source and destination
public sealed record OrderRequest(int Id, string Notes);
public sealed record Order(int Id, string Notes);
// 2. Declare the mapper — generator fills in the partial
[Map<OrderRequest, Order>]
public static partial class AppMappings { }
// 3. Use it
var order = AppMappings.Map(new OrderRequest(42, "rush"));What the generator emits is a single direct constructor call — no intermediate buffer, no boxing, no closure:
public static Order Map(OrderRequest src)
=> new Order(Id: src.Id, Notes: src.Notes);ZeroAlloc.Mapping matches Mapperly (the other source-generator mapper) on hot paths and is 2–18× faster than AutoMapper with allocation parity. Benchmarks below run on .NET 10.0.7, i9-12900HK, BenchmarkDotNet v0.15.8.
| Scenario | ZeroAlloc | Mapperly | AutoMapper | HandWritten |
|---|---|---|---|---|
| FlatIdentity (record copy) | 30.8 ns | 26.3 ns | 76.4 ns | 32.4 ns |
| FlatConversion (string→int) | 185.8 ns | 201.9 ns | 327.2 ns | 167.9 ns |
Flattening (a.b.c → d) |
22.9 ns | 23.0 ns | 70.8 ns | 29.2 ns |
| Polymorphic dispatch | 11.4 ns | 11.0 ns | 48.8 ns | 11.3 ns |
| UpdateInPlace | 6.1 ns | 6.0 ns | 83.9 ns (~18×) | 4.7 ns |
| TryMap (Result<T,Error>) | 208.4 ns | 195.4 ns (no native) | — | 222.6 ns |
| Collection (1000 items) | 38.6 µs | 41.1 µs | 30.4 µs | 39.6 µs |
Allocation: identical across mappers except Collection where AutoMapper allocates 11% more (86.5 KB vs 78.2 KB).
See docs/performance.md for the full BenchmarkDotNet output, methodology, and the enforced per-call allocation budgets (CI fails on regression).
[Map<TSrc, TDst>]— direct property/constructor mapping with case-sensitive matching and compile-time diagnostics on mismatch[TryMap<TSrc, TDst>]— fallible mapping returningResult<TDst, MappingError>(integrates with ZeroAlloc.Results smart constructors)[MapProperty]flattening —"Customer.Address.City" → Cityvia dotted paths[BeforeMap]/[AfterMap]hooks — zero overhead when bodies are empty[PolymorphicMap]— type-switch dispatch generated at compile time[ReverseMap]— symmetric Domain↔DTO with a single declaration- Update-in-place —
void Map(src, ref dst)overloads, zero destination allocation - Collection overloads —
List<T>,T[],IEnumerable<T>,IReadOnlyList<T>auto-emitted (opt-out via[SkipCollectionOverloads]) [MappingCulture]— culture-awaredecimal.Parse/DateTime.Parseper-mapper- ValueObjects integration — smart-constructor failures bubble through
TryMapasMappingError.SmartCtorRejected - Native AOT — no reflection, no
MakeGenericType, no expression compilation; trim/AOT warnings treated as errors in the AOT smoke gate
| Page | Description |
|---|---|
| Getting Started | Install + first mapper in five minutes |
| Basic Mapping | Property matching, conversions, customisation |
| Flattening | Dotted source paths via [MapProperty] |
| Collections | Auto-emitted overloads + nested elements |
| Polymorphic | Type-switch dispatch |
| Reverse Mapping | Symmetric Domain↔DTO |
| Update In Place | Allocation-free destination reuse |
| Hooks | [BeforeMap] / [AfterMap] |
| Culture & Strict | [MappingCulture] and strict mode |
| Performance | Benchmarks, allocation budgets, AOT |
| Diagnostics | All ZAMAP* diagnostic IDs |
| Testing | How to unit-test generated mappers |
| Advanced | Edge cases, generator internals |
MIT. See LICENSE.