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 13x faster with Mapster CodeGen!
Method | Mean | StdDev | Error | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|
'Mapster 4.1.1' | 1,388,744.4 us | 3,987.51 us | 6,028.54 us | 312000.0000 | - | - | 1251.22 MB |
'Mapster 4.1.1 (Codegen)' | 505,727.6 us | 2,525.21 us | 3,817.75 us | 312000.0000 | - | - | 1251.22 MB |
'ExpressMapper 1.9.1' | 3,122,200.4 us | 11,701.40 us | 19,663.45 us | 604000.0000 | - | - | 2418.52 MB |
'AutoMapper 9.0.0' | 6,883,546.7 us | 28,159.65 us | 42,573.37 us | 911000.0000 | - | - | 3646.85 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