Skip to main content

Mappa.Generator by Stefano Anelli

NuGet / site data​

Nuget GitHub last commit GitHub Repo stars

Details​

Info​

info

Name: Mappa.Generator

Mappa source generator.

Author: Stefano Anelli

NuGet: https://www.nuget.org/packages/Mappa.Generator/

You can find more details at https://github.com/sanelli/Mappa

Source: https://github.com/sanelli/Mappa

Author​

note

Stefano Anelli Alt text

Original Readme​

note

🗺️ Mappa​

Build main Line Coverage Branch Coverage Method Coverage

Mappa (italian for map) is a source generator for C# that can generate code to allow the mapping between types, similarly to what AutoMapper (and other similar tools) does. See Documentation for more details.

The main difference between Mappa and AutoMapper is that Mappa generates code at compile time while AutoMapper only at runtime; this has multiple benefits:

  • the code generated is optimised by the compiler;
  • the code generated is pure C# code that does not require any introspections;
  • the code generated works when AOT is required;
  • the code can be easily shared across different mappers;
  • fine-grained mapping can be obtained via attributes on the mapper methods without having to touch the source or the target classes;
  • mapper methods can be inside any class (static class and extension methods are supported!).
  • you do not need to specify every type that requires a mapping: if a mapping is missing Mappa will generate it for you;
🧑‍🔬 A simple example​

Consider the following code snippet:

using Mappa;
using System.Collections.Generic;

namespace Sample;

public enum MyEnumeration
{
One,
Two,
Three,
}

public class Source
{
public int PropertyA {get;set;}
public int[] PropertyB {get;set;}
public MyEnumeration PropertyC {get;set;}
}

public class Target
{
public long PropertyA {get;set;}
public List<string> PropertyB {get;set;}
public string PropertyC {get;set;}
}

[Mappa]
public partial class Mapper
{
public partial Target Map(Source input);
}

will create mapping code like the following:

namespace Sample;

public partial class Mapper
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("Mappa", "0.0.1.0")]
public partial Sample.Target Map(Sample.Source input)
{
long __mappa_tmp_1 = input.PropertyA;
int[] __mappa_tmp_2 = input.PropertyB;
System.Collections.Generic.List<string> __mappa_tmp_3 = new System.Collections.Generic.List<string>(__mappa_tmp_2.Length);
for (int __mappa_tmp_3 = 0; __mappa_tmp_3 < __mappa_tmp_2.Length; ++__mappa_tmp_3)
{
int __mappa_tmp_4 = __mappa_tmp_2[__mappa_tmp_3];
string __mappa_tmp_5 = __mappa_tmp_4.ToString();
__mappa_tmp_3.Add(__mappa_tmp_5)
}
Sample.MyEnumeration __mappa_tmp_6 = input.PropertyC;
string __mappa_tmp_7;
switch(__mappa_tmp_6)
{
case Sample.MyEnumeration.One:
__mappa_tmp_7 = "One";
break;
case Sample.MyEnumeration.Two:
__mappa_tmp_7 = "Two";
break;
case Sample.MyEnumeration.Three:
__mappa_tmp_7 = "Three";
break;
default:
throw new System.OutOfRangeException(nameof(__mappa_tmp_6));
}
Sample.Target __mappa_tmp_8 = new Sample.Target()
{
PropertyA = __mappa_tmp_1,
PropertyB = __mappa_tmp_3,
PropertyC = __mappa_tmp_7;
}
return __mappa_tmp_8;
}
}
🧑🏻‍💻 How to use Mappa​

The easiest way is to import the NuGet packages and apply the [Mappa] attribute on the partial classes that contain the partial methods that need to be generated.

Please see the tutorial in the documentation provided.

You can also find many examples in the Mappa.Samples project.

📦 NuGet packages​
  • Mappa: attributes and MappaContext used to drive the source generator;
  • Mappa source generator: source generator that automatically generates mappings between classes and value types.

Bson (ObjectId, Decimal128) and Protobuf (Timestamp, Duration, scalar wrappers) mappings are generated when you enable BsonWellKnownTypes / ProtobufWellKnownTypes and reference MongoDB.Bson or Google.Protobuf.

📈 Code coverage history​

Code coverage history

🏎️ Benchmarks​

Mappa is compared with AutoMapper, Mapster, and Mapperly on a shared set of mapping scenarios (collections, nested DTOs, IQueryable projection, and related cases). Lower time and lower memory are better. Details live in Mappa.Benchmark/README.md.

######### Latest run — winners

Benchmark winners

######### Latest run — mean time vs Mappa (%)

Benchmark mean time percentages

######### Latest run — allocated memory vs Mappa (%)

Benchmark allocated memory percentages

######### Latest run — mean time

Benchmark mean time

######### Latest run — allocated memory

Benchmark allocated memory

About​

note

Mapping from an object to another object

How to use​

Example (source csproj, source files)​

This is the CSharp Project that references Mappa.Generator

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Mappa" Version="10.3.0" />
<PackageReference Include="Mappa.Generator" Version="10.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>

Generated Files​

Those are taken from $(BaseIntermediateOutputPath)\GX

// <auto-generated>
// <tool-name>Mappa</tool-name>
// <tool-version>10.3.0.0</tool-version>
// <timestamp>Sun, 01 Nov 2026 19:28:38 GMT</timestamp>
// <note>This is an autogenerated file: edit to this file might be lost.</note>
// </auto-generated>

namespace mapperDemo;

public partial class UserMapper
{
#nullable enable
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Mappa", "10.3.0.0")]
public static partial mapperDemo.PersonDTO MapUser(Person from)
{
string? __mappa_tmp_1 = from.FirstName;
string? __mappa_tmp_2 = from.LastName;
mapperDemo.PersonDTO __mappa_tmp_3 = new mapperDemo.PersonDTO()
{
FirstName = __mappa_tmp_1,
LastName = __mappa_tmp_2,
};

return __mappa_tmp_3;
}
#nullable restore
}

Useful​

Download Example (.NET C#)​

Share Mappa.Generator​

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Mappa.Generator

Category "Mapper" has the following generators:​

1 AutoDTO Nuget GitHub Repo stars 2023-08-24

2 AutoGen Nuget GitHub Repo stars 2024-02-22

3 DynamicsMapper Nuget GitHub Repo stars 2023-10-16

4 Facet Nuget GitHub Repo stars 2025-08-17

5 LightweightObjectMapper Nuget GitHub Repo stars 2024-09-18

6 lomapper Nuget GitHub Repo stars 2026-04-07

7 MagicMap Nuget GitHub Repo stars 2023-10-08

8 MapItEasy Nuget GitHub Repo stars 2026-09-10

9 Mappa.Generator Nuget GitHub Repo stars 2026-10-03

10 mapperly Nuget GitHub Repo stars 2023-04-16

11 MapTo Nuget GitHub Repo stars 2023-10-05

12 Najlot.Map.SourceGenerator Nuget GitHub Repo stars 2026-08-29

13 NextGenMapper Nuget GitHub Repo stars 2023-08-16

See category​

Mapper