Writing mapping method is machine job. Do not waste your time, let Mapster do it.
PM> Install-Package Mapster
Mapster creates the destination object and maps values to it.
var destObject = sourceObject.Adapt<Destination>();
You make the object, Mapster maps to the object.
sourceObject.Adapt(destObject);
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 = p.Id,
Name = p.Name,
Surname = p.Surname,
....
})
.ToList();
}
- Two ways mapping
- Unflattening
- Map & ignore by property path
- MaxDepth
- Map to constructor
Mapster was designed to be efficient on both speed and memory. You could gain 5x faster while using only 1/3 of memory. And you could gain to 12x faster with
Method | Mean | StdDev | Error | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|
'Mapster 4.1.1' | 115.31 ms | 0.849 ms | 1.426 ms | 31000.0000 | - | - | 124.36 MB |
'Mapster 4.1.1 (Roslyn)' | 53.55 ms | 0.342 ms | 0.654 ms | 31100.0000 | - | - | 124.36 MB |
'Mapster 4.1.1 (FEC)' | 54.70 ms | 1.023 ms | 1.546 ms | 29600.0000 | - | - | 118.26 MB |
'Mapster 4.1.1 (Codegen)' | 48.22 ms | 0.868 ms | 1.312 ms | 31090.9091 | - | - | 124.36 MB |
'ExpressMapper 1.9.1' | 260.04 ms | 6.340 ms | 9.585 ms | 59000.0000 | - | - | 236.51 MB |
'AutoMapper 9.0.0' | 601.23 ms | 3.869 ms | 6.501 ms | 87000.0000 | - | - | 350.95 MB |
Step-into debugging lets you debug your mapping and inspect values as same as its your code.
Mapster CodeGen lets you do mapping with
- Validate mapping at compile time
- Getting raw performance
- Seeing your mapping code & debugging
- Finding usage of your models' properties
https://github.com/MapsterMapper/Mapster/releases