https://github.com/sanelli/mappa
Map your classes via source-generated code.
https://github.com/sanelli/mappa
csharp mapper source-generator
Last synced: about 1 year ago
JSON representation
Map your classes via source-generated code.
- Host: GitHub
- URL: https://github.com/sanelli/mappa
- Owner: sanelli
- License: mit
- Created: 2022-11-10T22:02:39.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-05-11T19:55:18.000Z (about 1 year ago)
- Last Synced: 2025-05-15T22:16:47.594Z (about 1 year ago)
- Topics: csharp, mapper, source-generator
- Language: C#
- Homepage:
- Size: 1.24 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 40
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
# πΊοΈ Mappa




Mappa (italian for _map_) is a source generator for `C#` that can generate code to allow the mapping between types, similarly to what [AutoMapper](https://www.nuget.org/packages/AutoMapper) (and other similar tools) does.
See [Documentation](./Documentation/README.md) for more details.
The main different 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 snipped:
```csharp
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 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:
```csharp
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 __mappa_tmp_3 = new System.Collections.Generic.List(__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 to is import the NuGet packages and apply the `[Mappa]` attribute on the partial classes that contains the partial methods that needs to be generated.
Please see the [tutorial](./Documentation/tutorial.md) in the [documentation](./Documentation/README.md) provided.
You can also find many examples in the [Mappa.Samples](Mappa.Samples) project.
## π¦ NuGet packages
- [Mappa](https://www.nuget.org/packages/Mappa/): source generator that allows to automatically generate mapping between classes and value types;
- [Mappa source generator](https://www.nuget.org/packages/Mappa.Generator/): source generator that allows to automatically generate mapping between classes and value types;
- [Mappa Protobuf](https://www.nuget.org/packages/Mappa.Dependency.Protobuf/): methods to map `Google.Protobuf.WellKnownTypes` objects from [Google.Protobuf](https://www.nuget.org/packages/Google.Protobuf) package into common objects.
- [Mappa Protobuf dependency](https://www.nuget.org/packages/Mappa.Dependency.Protobuf.DependencyInjection/): utility methods to register the Protobuf mapper.
- [Mappa Bson](https://www.nuget.org/packages/Mappa.Dependency.Bson/): methods to map `MongoDB.Bson` objects from [MongoDB.Bson](https://www.nuget.org/packages/MongoDB.Bson) package into common objects.
- [Mappa Bson dependency](https://www.nuget.org/packages/Mappa.Dependency.Bson.DependencyInjection/): utility methods to register the Bson mapper.
# π Code coverage history
- [Code Coverage gist](https://gist.github.com/sanelli/7f4a85bc809328b4821b03125f9190cb)
- [Code Coverage history](https://gist.github.com/sanelli/7f4a85bc809328b4821b03125f9190cb#file-mappa-code-coverage-history-md)
