ZeroAlloc.Mapping 1.4.1

dotnet add package ZeroAlloc.Mapping --version 1.4.1
                    
NuGet\Install-Package ZeroAlloc.Mapping -Version 1.4.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ZeroAlloc.Mapping" Version="1.4.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ZeroAlloc.Mapping" Version="1.4.1" />
                    
Directory.Packages.props
<PackageReference Include="ZeroAlloc.Mapping" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ZeroAlloc.Mapping --version 1.4.1
                    
#r "nuget: ZeroAlloc.Mapping, 1.4.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ZeroAlloc.Mapping@1.4.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ZeroAlloc.Mapping&version=1.4.1
                    
Install as a Cake Addin
#tool nuget:?package=ZeroAlloc.Mapping&version=1.4.1
                    
Install as a Cake Tool

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.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.4.1 361 5/12/2026
1.4.0 318 5/10/2026
1.3.1 217 5/9/2026
1.3.0 103 5/8/2026
1.2.0 100 5/8/2026
1.1.0 105 5/8/2026
1.0.0 97 5/8/2026