mapperly by Riok
Nuget / site data
Details
Info
Name: mapperly
A .NET source generator for generating object mappings. Trimming save. Inspired by MapStruct.
Author: Riok
NuGet: https://www.nuget.org/packages/Riok.Mapperly/
You can find more details at https://mapperly.riok.app/docs/getting-started/installation/
Source : https://github.com/riok/mapperly
Original Readme
Mapperly
Mapperly is a .NET source generator for generating object mappings. Inspired by MapStruct.
Because Mapperly creates the mapping code at build time, there is minimal overhead at runtime. Even better, the generated code is perfectly readable, allowing you to verify the generated mapping code easily.
Documentation
The documentation is available here.
Quickstart
Installation
Add the NuGet Package to your project:
dotnet add package Riok.Mapperly
Create your first mapper
Create a mapper declaration as a partial class
and apply the Riok.Mapperly.Abstractions.MapperAttribute
attribute.
Mapperly generates mapping method implementations for the defined mapping methods in the mapper.
// Mapper declaration
[Mapper]
public partial class CarMapper
{
public partial CarDto CarToCarDto(Car car);
}
// Mapper usage
var mapper = new CarMapper();
var car = new Car { NumberOfSeats = 10, ... };
var dto = mapper.CarToCarDto(car);
dto.NumberOfSeats.Should().Be(10);
Read the docs for any further information.
How To Contribute
We would love for you to contribute to Mapperly and help make it even better than it is today! Find information on how to contribute in the docs.
License
Mapperly is Apache 2.0 licensed.
About
Mapping classes to/from DTO
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Person.cs
- PersonDTO.cs
- PersonMapper.cs
This is the CSharp Project that references mapperly
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Riok.Mapperly" Version="2.8.0" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of mapperly in Program.cs
// See https://aka.ms/new-console-template for more information
using mapperlyDemo;
var p=new Person();
p.FirstName = "Andrei";
p.LastName = "Ignat";
PersonMapper mapper = new() ;
var dto=mapper.Person2PersonDTO(p);
Console.WriteLine(dto.FullName);
This is the use of mapperly in Person.cs
public class Person
{
public int ID { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
}
This is the use of mapperly in PersonDTO.cs
namespace mapperlyDemo;
public class PersonDTO
{
public int ID { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string FullName {
get
{
return FirstName + " " + LastName;
}
}
}
This is the use of mapperly in PersonMapper.cs
using Riok.Mapperly.Abstractions;
namespace mapperlyDemo;
[Mapper]
public partial class PersonMapper
{
public partial PersonDTO Person2PersonDTO(Person p);
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- PersonMapper.g.cs
#nullable enable
namespace mapperlyDemo
{
public partial class PersonMapper
{
public partial global::mapperlyDemo.PersonDTO Person2PersonDTO(global::Person p)
{
var target = new global::mapperlyDemo.PersonDTO();
target.ID = p.ID;
target.FirstName = p.FirstName;
target.LastName = p.LastName;
return target;
}
}
}
Usefull
Download Example (.NET C# )
Share mapperly
https://ignatandrei.github.io/RSCG_Examples/v2/docs/mapperly