Writing mapping methods is a machine job. Do not waste your time, let Mapster do it.
Install Mapster with the NuGet CLI:
Or use the .NET core CLI to install Mapster:
dotnet add package Mapster
Mapster creates the destination object and maps values to it.
var destObject = sourceObject.Adapt<Destination>();Mapping to an existing object
You create the object, Mapster maps to the object.
sourceObject.Adapt(destObject);You can get IMapper instance via dependency injection so you do not have to change code when migrating to mapster from automapper
Add Mapster to service collection
And use it with DI
public class Test { public Test(IMapper mapper) { var sourceObject = mapper.Adapt<Destination>(); } }
Mapster also provides extensions to map queryables.
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.
[AdaptTo("[name]Dto"), GenerateMapper] public class Student { ... }
Then Mapster will generate:
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 => ... }
ISet
, IDictionary
, IReadOnlyDictionary
supportEmptyCollectionIfNull
, CreateNewIfNull
DestinationTransformMapster 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
Step-into debugging lets you debug your mapping and inspect values just like your code.
Code generation allows you to
There are currently two tools which you can choose based on your preferences.
https://github.com/MapsterMapper/Mapster/releases
JetBrains kindly provides Mapster with a free open-source licence for their Resharper and Rider.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4