Back to Mapster

Mapster - The Mapper of Your Domain

README.md

10.0.79.3 KB
Original Source

Mapster - The Mapper of Your Domain

Writing mapping methods is a machine job. Do not waste your time, let Mapster do it.

NuGet Sources

NuGet Packages

PackageStablePre-release
Mapster
Mapster.Core
Mapster.DependencyInjection
Mapster.EFCore
Mapster.EF6
Mapster.JsonNet
Mapster.Immutable
Mapster.Diagnostics
ExpressionDebugger

DotNet Tools

ToolStablePre-release
Mapster.Tool

Installation

Install Mapster with the NuGet CLI:

Powershell
Install-Package Mapster

Or use the .NET core CLI to install Mapster:

bash
dotnet add package Mapster --project <PROJECT_NAME>

Basic usage

Mapping to a new object

Mapster creates the destination object and maps values to it.

csharp
var destObject = sourceObject.Adapt<Destination>();

Mapping to an existing object

You create the object, Mapster maps to the object.

csharp
sourceObject.Adapt(destObject);

Use Mapster with Dependency Injection

You can get your IMapper instance via dependency injection, so you do not have to change code, when migrating to mapster from automapper!

Just add Mapster to service collection:

csharp
services.AddMapster();

And use it with DI in your Project:

csharp
public class Test
{
    public Test(IMapper mapper)
    {
        var sourceObject = mapper.Adapt<Destination>();
    }
}

Queryable Extensions

Mapster also provides extensions to map queryables.

csharp
using (MyDbContext context = new MyDbContext())
{
    // Build a Select Expression from DTO
    var destinations = context.Sources.ProjectToType<Destination>().ToList();

    // Versus creating by hand:
    var destinations = context.Sources.Select(c => new Destination {
        Id = c.Id,
        Name = c.Name,
        Surname = c.Surname,
        ....
    })
    .ToList();
}

Generating models & mappers

No need to write your own DTO classes. Mapster provides Mapster.Tool to help you generating models. And if you would like to have explicit mapping, Mapster also generates mapper class for you.

csharp
[AdaptTo("[name]Dto"), GenerateMapper]
public class Student {
    ...
}

Then Mapster will generate for you:

csharp
public class StudentDto {
    ...
}
public static class StudentMapper {
    public static StudentDto AdaptToDto(this Student poco) { ... }
    public static StudentDto AdaptTo(this Student poco, StudentDto dto) { ... }
    public static Expression<Func<Student, StudentDto>> ProjectToDto => ...
}

What's new

Why Mapster?

Performance & Memory efficient

Mapster was designed to be efficient on both speed and memory. You could gain a 4x performance improvement whilst using only 1/3 of memory. And you could gain up to 12x faster performance with:

MethodMeanStdDevErrorGen 0Gen 1Gen 2Allocated
'Mapster 6.0.0'108.59 ms1.198 ms1.811 ms31000.0000--124.36 MB
'Mapster 6.0.0 (Roslyn)'38.45 ms0.494 ms0.830 ms31142.8571--124.36 MB
'Mapster 6.0.0 (FEC)'37.03 ms0.281 ms0.472 ms29642.8571--118.26 MB
'Mapster 6.0.0 (Codegen)'34.16 ms0.209 ms0.316 ms31133.3333--124.36 MB
'ExpressMapper 1.9.1'205.78 ms5.357 ms8.098 ms59000.0000--236.51 MB
'AutoMapper 10.0.0'420.97 ms23.266 ms35.174 ms87000.0000--350.95 MB

Step into debugging

Step-into debugging lets you debug your mapping and inspect values just like your code.

Code Generation

Code generation allows you to

  • Validate mapping at compile time
  • Getting raw performance
  • Seeing your mapping code & debugging
  • Finding usage of your models' properties

There are currently two tools which you can choose based on your preferences.

Change logs

https://github.com/MapsterMapper/Mapster/releases

Usage Guides with Translations

Acknowledgements

JetBrains kindly provides Mapster with a free open-source licence for their Resharper and Rider.

  • Resharper makes Visual Studio a much better IDE
  • Rider is fast & powerful cross platform .NET IDE