Home

Awesome

MapperToolkit

Nuget GitHub

This project is a Mapper library use Source Generator with function such as:

MapperToolkit is the fastest .NET object mapper if member inclube List IEnumerable surpassing even the manual mapping and Mapperly The benchmark was generated with(https://github.com/GaoNian-NET/Benchmark.netCoreMappers).

MethodMeanErrorStdDevGen 0Gen 1Allocated
AgileMapper2,832.5 ns56.29 ns80.73 ns0.50350.00383 KB
TinyMapper3,195.5 ns62.88 ns113.39 ns0.3853-2 KB
ExpressMapper2,957.5 ns84.74 ns248.54 ns0.80490.00385 KB
AutoMapper1,497.6 ns33.87 ns98.79 ns0.34520.00192 KB
ManualMapping459.0 ns14.02 ns41.13 ns0.21030.00051 KB
Mapster465.3 ns13.84 ns40.60 ns0.32850.00192 KB
Mapperly484.8 ns9.02 ns16.71 ns0.26130.00102 KB
MapperToolkit303.2 ns6.11 ns5.10 ns0.18840.00051 KB

Quickstart

Installation

Add the NuGet Package to your project:

dotnet add package MapperToolkit

Create your first mapper

//example Entity
public class Entity
{
    public string Account { get; set; }
    public DateTime CreateTime { get; set; }
}

//example DTO
public class DTO
{
    public string EmpNo { get; set; }
    public string EmpName { get; set; }
    public DateTime CreateTime { get; set; }
}

Create profile class inherit MapperToolkit.Profile in constructors type mapper code

// Profile declaration
public class MappingProfile : Profile
{
    public MappingProfile()
    {
        GenerateAllMapper<Entity, DTO>()
        .Map(src => src.Account[0..4],dest=>dest.EmpNo)
        .Map(src => src.Account[4..^0],dest=>dest.EmpName);
    }
}

//Generator code
 public static partial class DTOMapper
{
    public static global::ConsoleApp.DTO MapperToDTO(this global::ConsoleApp.Entity source)
    {
        global::ConsoleApp4.DTO result = new();
        global::System.Func<global::ConsoleApp.Entity, string> EmpNoMapper = src => src.Account[0..4];
        global::System.Func<global::ConsoleApp.Entity, string> EmpNameMapper = src => src.Account[4..^0];
        result.EmpNo = EmpNoMapper.Invoke(source);
        result.EmpName = EmpNameMapper.Invoke(source);
        result.CreateTime = source.CreateTime;
        return result;
    }
}
//Mapper usage
var entity = new Entity() { Account = "A123Bojack" ,CreateTime = DateTime.Now };
var dto = entity.MapperToDTO();
Console.WriteLine($"EmpNo:{dto.EmpNo};EmpName:{dto.EmpName};CreateTime:{dto.CreateTime}");
// print EmpNo:A123;EmpName:Bojack;CreateTime:2022/8/16 12:13:15