Skip to content

ZeroAlloc-Net/ZeroAlloc.Mapping

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

135 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ZeroAlloc.Mapping

NuGet Build License: MIT AOT GitHub Sponsors

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.

Install

dotnet add package ZeroAlloc.Mapping

The generator runs automatically as part of dotnet build — there is no DI registration or runtime configuration.

Example

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);

Performance

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).

Features

  • [Map<TSrc, TDst>] — direct property/constructor mapping with case-sensitive matching and compile-time diagnostics on mismatch
  • [TryMap<TSrc, TDst>] — fallible mapping returning Result<TDst, MappingError> (integrates with ZeroAlloc.Results smart constructors)
  • [MapProperty] flattening"Customer.Address.City" → City via 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-placevoid Map(src, ref dst) overloads, zero destination allocation
  • Collection overloadsList<T>, T[], IEnumerable<T>, IReadOnlyList<T> auto-emitted (opt-out via [SkipCollectionOverloads])
  • [MappingCulture] — culture-aware decimal.Parse / DateTime.Parse per-mapper
  • ValueObjects integration — smart-constructor failures bubble through TryMap as MappingError.SmartCtorRejected
  • Native AOT — no reflection, no MakeGenericType, no expression compilation; trim/AOT warnings treated as errors in the AOT smoke gate

Documentation

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

License

MIT. See LICENSE.

About

Source-generated, zero-allocation, AOT-safe Command→Domain→DTO mapper for .NET. [Map<,>]/[TryMap<,>] on a static partial class — generator emits direct property assignments at compile time. Result<T, MappingError> on the [TryMap] path; integrates with ZeroAlloc.Results and ZeroAlloc.ValueObjects smart constructors.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors