Riok.Mapperly 2.0.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Riok.Mapperly --version 2.0.0                
NuGet\Install-Package Riok.Mapperly -Version 2.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Riok.Mapperly" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Riok.Mapperly --version 2.0.0                
#r "nuget: Riok.Mapperly, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Riok.Mapperly as a Cake Addin
#addin nuget:?package=Riok.Mapperly&version=2.0.0

// Install Riok.Mapperly as a Cake Tool
#tool nuget:?package=Riok.Mapperly&version=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);
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (16)

Showing the top 5 NuGet packages that depend on Riok.Mapperly:

Package Downloads
Rocket.Surgery.LaunchPad.Mapping

Package Description

Indice.Hive.Core

Package Description

Fanzoo.Kernel

An opinionated framework for building scalable web applications.

AssemblyAI

The AssemblyAI C# .NET SDK provides an easy-to-use interface for interacting with the AssemblyAI API, which supports async and real-time transcription, audio intelligence models, as well as the latest LeMUR models.

Eternet.AspNetCore.DocumentDb.Crud

AspNet Core DocumentDb CRUD framework using Marten for Document DB and Events Store

GitHub repositories (18)

Showing the top 5 popular GitHub repositories that depend on Riok.Mapperly:

Repository Stars
riok/mapperly
A .NET source generator for generating object mappings. No runtime reflection.
martinothamar/Mediator
A high performance implementation of Mediator pattern in .NET using source generators.
jeremytammik/RevitLookup
Interactive Revit RFA and RVT project database exploration tool to view and navigate BIM element parameters, properties and relationships.
bitfoundation/bitplatform
Build all of your apps using what you already know and love ❤️
ldqk/Masuit.MyBlogs
基于C#/.NET8的 masuit.org个人博客站项目源码,https://masuit.org ,供参考、学习、引用、非商业性质的部署。
Version Downloads Last updated
4.2.0-next.0 1,239 1/20/2025
4.1.1 340,857 11/20/2024
4.1.1-next.0 8,464 11/5/2024
4.1.0 216,844 10/31/2024
4.1.0-next.3 141 10/29/2024
4.1.0-next.2 968 10/25/2024
4.1.0-next.1 637 10/14/2024
4.1.0-next.0 1,928 10/11/2024
4.0.0 156,176 10/11/2024
4.0.0-next.4 2,086 9/29/2024
4.0.0-next.3 16,529 8/13/2024
4.0.0-next.2 487 8/6/2024
4.0.0-next.1 6,237 6/30/2024
3.6.0 1,111,666 6/18/2024
3.6.0-next.2 1,886 6/3/2024
3.6.0-next.1 5,913 5/3/2024
3.5.1 454,470 4/23/2024
3.5.1-next.2 1,990 4/15/2024
3.5.1-next.1 235 4/9/2024
3.5.0 284,770 4/5/2024
3.5.0-next.4 1,277 3/27/2024
3.5.0-next.3 9,818 3/18/2024
3.5.0-next.2 19,534 3/11/2024
3.5.0-next.1 654 3/9/2024
3.4.0 390,354 2/23/2024
3.4.0-next.5 1,721 2/20/2024
3.4.0-next.4 898 2/16/2024
3.4.0-next.3 4,559 2/5/2024
3.4.0-next.2 20,247 1/10/2024
3.4.0-next.1 335 1/6/2024
3.3.1-next.1 2,942 12/18/2023
3.3.0 586,966 12/12/2023
3.3.0-next.6 1,025 12/6/2023
3.3.0-next.5 1,387 11/28/2023
3.3.0-next.4 2,623 11/22/2023
3.3.0-next.3 7,993 11/19/2023
3.3.0-next.2 3,508 10/26/2023
3.3.0-next.1 3,772 10/11/2023
3.2.0 677,553 9/18/2023
3.2.0-next.4 1,404 9/12/2023
3.2.0-next.3 467 9/11/2023
3.2.0-next.2 352 9/5/2023
3.2.0-next.1 488 8/29/2023
3.1.0 154,840 8/18/2023
3.1.0-next.2 116 8/18/2023
3.1.0-next.1 4,592 8/8/2023
3.0.0 42,214 8/7/2023
3.0.0-next.1 373 8/2/2023
2.9.0-next.4 6,889 7/26/2023
2.9.0-next.3 767 7/16/2023
2.9.0-next.2 5,073 6/13/2023
2.9.0-next.1 5,574 5/11/2023
2.8.0 571,068 4/27/2023
2.8.0-next.2 527 4/20/2023
2.8.0-next.1 17,591 3/23/2023
2.7.1-next.1 667 3/17/2023
2.7.0 174,606 3/13/2023
2.7.0-next.2 839 1/26/2023
2.7.0-next.1 618 1/23/2023
2.6.0 93,979 1/12/2023
2.6.0-next.4 121 1/11/2023
2.6.0-next.3 178 1/9/2023
2.6.0-next.2 299 12/14/2022
2.6.0-next.1 576 12/12/2022
2.5.0 87,559 10/12/2022
2.5.0-next.2 344 9/28/2022
2.5.0-next.1 245 9/19/2022
2.4.1-next.1 213 9/16/2022
2.4.0 20,503 9/8/2022
2.3.3 3,115 8/10/2022
2.3.2 924 8/9/2022
2.3.1 61,613 5/31/2022
2.3.0 2,427 5/16/2022
2.2.1 3,544 4/6/2022
2.2.0 1,269 3/15/2022
2.1.0 1,083 2/28/2022
2.0.0 1,174 2/21/2022