Riok.Mapperly 1.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 an interface or abstract class and apply the Riok.Mapperly.Abstractions.MapperAttribute attribute. Mapperly generates mapping method implementations for the defined mapping methods in the mapper. The default implementation name is built removing the leading I if it is an interface (IDtoMapper => DtoMapper) and appending Impl if the mapper definition is an abstract class or the interface name does not start with an I. A mapper instance is available through the Instance field on the generated implementation.

// mapper declaration
[Mapper]
public interface IDtoMapper
{
    CarDto CarToCarDto(Car car);
}

// mapper usage
var car = new Car { NumberOfSeats = 10, ... };
var dto = DtoMapper.Instance.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 interface IDtoMapper
{
    void CarToCarDto(Car car, CarDto dto);
}

// mapper usage
var car = new Car { NumberOfSeats = 10, ... };
var dto = new CarDto();
DtoMapperImpl.Instance.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 interface IDtoMapper
{
    CarDto CarToCarDto(Car car);

    DateOnly DateTimeToDateOnly(DateTime dt) => DateOnly.FromDateTime(dt);
}

Whenever Mapperly needs a mapping from DateTime to DateOnly inside the IDtoMapper implementation it will use the provided implementation.

Showing the top 20 packages that depend on Riok.Mapperly.

Packages Downloads
Volo.Abp.Mapperly
Package Description
4
Volo.Abp.Mapperly
Package Description
3

# 1.0.0 (2022-02-17) ### Bug Fixes * consider a type as immutable only if it is a read only type or a string ([#9](https://github.com/riok/mapperly/issues/9)) ([93d8eec](https://github.com/riok/mapperly/commit/93d8eeca083046c0031a80301cdcfd718f35c38b)) * deep clone for object casts and add tests ([#13](https://github.com/riok/mapperly/issues/13)) ([0fe04ec](https://github.com/riok/mapperly/commit/0fe04ec7ee2170a482acacbe0d8911f4f157a228)) * improve nullable handling ([#15](https://github.com/riok/mapperly/issues/15)) ([17e1df5](https://github.com/riok/mapperly/commit/17e1df554728e2973f387ff9a8d980d81852452d)) * unique method names ([#14](https://github.com/riok/mapperly/issues/14)) ([1b290b1](https://github.com/riok/mapperly/commit/1b290b13b43e9c27eca463d23e5f564d9486d660)) ### Features * add configuration whether to deep clone objects ([#10](https://github.com/riok/mapperly/issues/10)) ([baf98fd](https://github.com/riok/mapperly/commit/baf98fdd0929f646637efebc9d638ea27dd4456d)) * **enum:** add ignore case option for enum mappings ([#12](https://github.com/riok/mapperly/issues/12)) ([2d73020](https://github.com/riok/mapperly/commit/2d730206346504269afaf88fd7713c2e7bc6d844)) * **enumerables:** optimize array mapping by using Array.Clone() in certain cases ([#7](https://github.com/riok/mapperly/issues/7)) ([89f96c4](https://github.com/riok/mapperly/commit/89f96c4629744088750cfeba87631876f238c2a2)) * Initial commit ([#1](https://github.com/riok/mapperly/issues/1)) ([ad539c8](https://github.com/riok/mapperly/commit/ad539c824a6e2920d49a2fac860d832e3cf269fc))

.NET Standard 2.0

  • No dependencies.

Version Downloads Last updated
4.3.1-next.0 2 12/12/2025
4.3.0 5 10/28/2025
4.3.0-next.6 5 10/28/2025
4.3.0-next.5 5 10/14/2025
4.3.0-next.4 5 10/15/2025
4.3.0-next.3 7 09/05/2025
4.3.0-next.2 8 07/17/2025
4.3.0-next.1 8 07/17/2025
4.3.0-next.0 9 05/31/2025
4.2.1 11 05/31/2025
4.2.1-next.2 9 05/31/2025
4.2.1-next.1 9 05/31/2025
4.2.1-next.0 9 05/31/2025
4.2.0 10 05/31/2025
4.2.0-next.2 9 05/31/2025
4.2.0-next.1 10 03/08/2025
4.2.0-next.0 11 03/08/2025
4.1.1 12 03/07/2025
4.1.1-next.0 11 03/08/2025
4.1.0 12 03/07/2025
4.1.0-next.3 11 03/08/2025
4.1.0-next.2 11 03/08/2025
4.1.0-next.1 12 03/08/2025
4.1.0-next.0 11 03/08/2025
4.0.0 12 03/07/2025
4.0.0-next.4 11 03/08/2025
4.0.0-next.3 11 03/08/2025
4.0.0-next.2 11 03/08/2025
4.0.0-next.1 11 03/08/2025
3.6.0 12 03/07/2025
3.6.0-next.2 11 03/08/2025
3.6.0-next.1 11 03/08/2025
3.5.1 13 04/30/2024
3.5.1-next.2 12 05/01/2024
3.5.1-next.1 12 05/01/2024
3.5.0 13 04/30/2024
3.5.0-next.4 12 05/01/2024
3.5.0-next.3 13 03/25/2024
3.5.0-next.2 13 03/25/2024
3.5.0-next.1 13 03/25/2024
3.4.0 14 03/25/2024
3.4.0-next.5 13 03/25/2024
3.4.0-next.4 15 02/16/2024
3.4.0-next.3 14 02/16/2024
3.4.0-next.2 14 02/16/2024
3.4.0-next.1 14 02/16/2024
3.3.1-next.1 14 02/16/2024
3.3.0 15 02/16/2024
3.3.0-next.6 14 02/16/2024
3.3.0-next.5 14 02/16/2024
3.3.0-next.4 14 02/16/2024
3.3.0-next.3 14 02/16/2024
3.3.0-next.2 14 02/16/2024
3.3.0-next.1 14 02/16/2024
3.2.0 16 02/16/2024
3.2.0-next.4 14 02/16/2024
3.2.0-next.3 14 02/16/2024
3.2.0-next.2 14 02/16/2024
3.2.0-next.1 13 02/16/2024
3.1.0 15 02/16/2024
3.1.0-next.2 14 02/16/2024
3.1.0-next.1 13 02/16/2024
3.0.0 15 02/16/2024
3.0.0-next.1 14 02/16/2024
2.9.0-next.4 14 02/16/2024
2.9.0-next.3 14 02/16/2024
2.9.0-next.2 93 01/21/2024
2.9.0-next.1 14 02/16/2024
2.8.0 15 02/16/2024
2.8.0-next.2 14 02/16/2024
2.8.0-next.1 14 02/16/2024
2.7.1-next.1 14 02/16/2024
2.7.0 17 02/16/2024
2.7.0-next.2 14 02/16/2024
2.7.0-next.1 14 02/16/2024
2.6.0 15 02/16/2024
2.6.0-next.4 14 02/16/2024
2.6.0-next.3 14 02/16/2024
2.6.0-next.2 14 02/16/2024
2.6.0-next.1 14 02/16/2024
2.5.0 15 02/16/2024
2.5.0-next.2 14 02/16/2024
2.5.0-next.1 14 02/16/2024
2.4.1-next.1 15 02/16/2024
2.4.0 15 02/16/2024
2.3.3 15 02/16/2024
2.3.2 14 02/16/2024
2.3.1 15 02/16/2024
2.3.0 14 02/16/2024
2.2.1 15 02/16/2024
2.2.0 15 02/16/2024
2.1.0 14 02/16/2024
2.0.0 15 02/16/2024
1.0.0 13 06/17/2024