Riok.Mapperly 2.0.0
Mapperly
A .NET source generator for generating object mappings. Inspired by MapStruct.
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 DtoMapper
{
public partial CarDto CarToCarDto(Car car);
}
// mapper usage
var mapper = new DtoMapper();
var car = new Car { NumberOfSeats = 10, ... };
var dto = mapper.CarToCarDto();
dto.NumberOfSeats.Should().Be(10);
Configuration
The attributes defined in Riok.Mapperly.Abstractions
can be used to customize different aspects of the generated mapper.
Mapper
The MapperAttribute
provides options to customize the generated mapper class.
The generated class name, the instance field name and the default enum mapping strategy is adjustable.
Copy behaviour
By default, Mapperly does not create deep copies of objects to improve performance.
If an object can be directly assigned to the target, it will do so
(eg. if the source and target type are both Car[]
, the array and its entries will not be cloned).
To create deep copies, set the UseDeepCloning
property on the MapperAttribute
to true
.
Properties
On each mapping method declaration property mappings can be customized.
If a property on the target has a different name than on the source, the MapPropertyAttribute
can be applied.
Flattening is not yet supported.
If a property should be ignored, the MapperIgnoreAttribute
can be used.
Enum
The enum mapping can be customized by setting the strategy to use.
Apply the MapEnumAttribute
and pass the strategy to be used for this enum.
It is also possible to set the strategy for the entire mapper via the MapperAttribute
.
Available strategies:
Name | Description |
---|---|
ByValue | Matches enum entries by their values |
ByName | Matches enum entries by their exact names |
The IgnoreCase
property allows to opt in for case ignored mappings.
Void mapping methods
If an existing object instance should be used as target you can define the mapping method as void with the target as second parameter:
// mapper declaration
[Mapper]
public partial class DtoMapper
{
public partial void CarToCarDto(Car car, CarDto dto);
}
// mapper usage
var mapper = new DtoMapper();
var car = new Car { NumberOfSeats = 10, ... };
var dto = new CarDto();
mapper.CarToCarDto(car, dto);
dto.NumberOfSeats.Should().Be(10);
User implemented mapping methods
If Mapperly cannot generate a mapping, one can implement it manually simply by providing a method body in the mapper declaration:
[Mapper]
public partial class DtoMapper
{
public partial CarDto CarToCarDto(Car car);
private DateOnly DateTimeToDateOnly(DateTime dt) => DateOnly.FromDateTime(dt);
}
Whenever Mapperly needs a mapping from DateTime
to DateOnly
inside the DtoMapper
implementation it will use the provided implementation.
Before / after map
To run custom code before or after a mapping, the generated map method can be wrapped in a custom method:
[Mapper]
public partial class DtoMapper
{
public CarDto MapCarToCarDto(Car car)
{
// custom before map code...
var dto = CarToCarDto(car);
// custom after map code...
return dto;
}
private partial CarDto CarToCarDto(Car car);
}
No packages depend on Riok.Mapperly.
.NET Standard 2.0
- No dependencies.
Version | Downloads | Last updated |
---|---|---|
4.2.1 | 2 | 04/28/2025 |
4.2.1-next.2 | 1 | 04/25/2025 |
4.2.1-next.1 | 1 | 04/25/2025 |
4.2.1-next.0 | 5 | 04/14/2025 |
4.2.0 | 6 | 03/31/2025 |
4.2.0-next.2 | 7 | 03/23/2025 |
4.2.0-next.1 | 8 | 02/21/2025 |
4.2.0-next.0 | 8 | 01/26/2025 |
4.1.1 | 16 | 11/23/2024 |
4.1.1-next.0 | 16 | 11/07/2024 |
4.1.0 | 15 | 10/31/2024 |
4.1.0-next.3 | 14 | 10/31/2024 |
4.1.0-next.2 | 14 | 10/26/2024 |
4.1.0-next.1 | 15 | 10/16/2024 |
4.1.0-next.0 | 20 | 10/11/2024 |
4.0.0 | 17 | 10/11/2024 |
4.0.0-next.4 | 14 | 10/02/2024 |
4.0.0-next.3 | 20 | 08/17/2024 |
4.0.0-next.2 | 21 | 08/12/2024 |
4.0.0-next.1 | 20 | 07/01/2024 |
3.6.0 | 1,999 | 06/19/2024 |
3.6.0-next.2 | 24 | 06/06/2024 |
3.6.0-next.1 | 23 | 05/03/2024 |
3.5.1 | 23 | 04/23/2024 |
3.5.1-next.2 | 22 | 04/19/2024 |
3.5.1-next.1 | 21 | 04/09/2024 |
3.5.0 | 23 | 04/05/2024 |
3.5.0-next.4 | 23 | 03/27/2024 |
3.5.0-next.3 | 25 | 03/18/2024 |
3.5.0-next.2 | 21 | 03/16/2024 |
3.5.0-next.1 | 23 | 03/10/2024 |
3.4.0 | 21 | 03/01/2024 |
3.4.0-next.5 | 21 | 02/28/2024 |
3.4.0-next.4 | 21 | 02/19/2024 |
3.4.0-next.3 | 25 | 02/13/2024 |
3.4.0-next.2 | 26 | 01/12/2024 |
3.4.0-next.1 | 23 | 01/12/2024 |
3.3.1-next.1 | 27 | 12/21/2023 |
3.3.0 | 1,984 | 12/16/2023 |
3.3.0-next.6 | 27 | 12/10/2023 |
3.3.0-next.5 | 23 | 12/06/2023 |
3.3.0-next.4 | 24 | 12/06/2023 |
3.3.0-next.3 | 26 | 12/06/2023 |
3.3.0-next.2 | 24 | 10/26/2023 |
3.3.0-next.1 | 21 | 10/22/2023 |
3.2.0 | 25 | 09/19/2023 |
3.2.0-next.4 | 25 | 09/25/2023 |
3.2.0-next.3 | 25 | 09/12/2023 |
3.2.0-next.2 | 26 | 09/23/2023 |
3.2.0-next.1 | 24 | 09/03/2023 |
3.1.0 | 29 | 08/26/2023 |
3.1.0-next.2 | 26 | 08/23/2023 |
3.1.0-next.1 | 27 | 08/16/2023 |
3.0.0 | 26 | 08/16/2023 |
3.0.0-next.1 | 29 | 08/17/2023 |
2.9.0-next.4 | 25 | 08/06/2023 |
2.9.0-next.3 | 27 | 08/10/2023 |
2.9.0-next.2 | 24 | 07/05/2023 |
2.9.0-next.1 | 24 | 07/05/2023 |
2.8.0 | 28 | 07/05/2023 |
2.8.0-next.2 | 28 | 07/06/2023 |
2.8.0-next.1 | 491 | 04/13/2023 |
2.7.1-next.1 | 27 | 07/10/2023 |
2.7.0 | 29 | 07/08/2023 |
2.7.0-next.2 | 25 | 07/07/2023 |
2.7.0-next.1 | 23 | 07/07/2023 |
2.6.0 | 24 | 07/05/2023 |
2.6.0-next.4 | 25 | 07/04/2023 |
2.6.0-next.3 | 23 | 07/08/2023 |
2.6.0-next.2 | 27 | 07/07/2023 |
2.6.0-next.1 | 27 | 07/07/2023 |
2.5.0 | 30 | 06/27/2023 |
2.5.0-next.2 | 25 | 07/09/2023 |
2.5.0-next.1 | 28 | 07/06/2023 |
2.4.1-next.1 | 26 | 07/07/2023 |
2.4.0 | 25 | 07/05/2023 |
2.3.3 | 28 | 07/11/2023 |
2.3.2 | 27 | 07/07/2023 |
2.3.1 | 30 | 07/06/2023 |
2.3.0 | 31 | 07/05/2023 |
2.2.1 | 32 | 07/06/2023 |
2.2.0 | 27 | 07/04/2023 |
2.1.0 | 26 | 07/03/2023 |
2.0.0 | 27 | 07/17/2023 |