Opininated (yet another) mapper, mainly to convert between EF Entities and DTOs.
Let's first obey the number one rule for mappers, a benchmark (using BenchmarkDotNet):
| Method | Mean |
|---|---|
| BatMap 💥 | 1.3309 ms 💥 |
| Mapster | 2.0377 ms |
| SafeMapper | 2.0371 ms |
| HandWritten | 2.1175 ms |
| AutoMapper | 2.6187 ms |
| TinyMapper | 2.8119 ms |
| ExpressMapper | 5.0814 ms |
| FastMapper | 5.9380 ms |
Results may (probably) vary. Latest run can bee seen on Appveyor Build.
- Fast (enough)
- NOT over-engineered, code is really simple
- Instantiatable mapper
- Convention based, zero configuration static shortcut exists too (obviously named Mapper)
- Does not crash when faced with circular-dependencies during registration
- In fact, can resolve recurring instances to same target instance (yaay no StackOverflowException!)
- Can project IQueryable<TSource> to IQueryable<TTarget> with respect to includes (via auto-detection or with custom parameters)
- and much more...
Registration with static API:
Mapper.RegisterMap<Customer, CustomerDTO>();or use an instance:
var mapper = new MapConfiguration(dynamicMapping: DynamicMapping.MapAndCache, preserveReferences: true);
mapper.RegisterMap<Customer, CustomerDTO>();Note: You don't have to register type mappings when using a MapConfiguration with Dynamic Mapping enabled (like the static API uses).
You can customize expressions for members:
mapper.RegisterMap<Order, OrderDTO>(b => b.MapMember(o => o.Price, (o, mc) => o.Count * o.UnitPrice));Map an object:
Mapper.Map<CustomerDTO>(customer);Map an enumerable:
customers.MapTo<Customer, CustomerDTO>(preserveReferences: true); // extension methods FTW!Project a query:
customerQuery.ProjectTo<CustomerDTO>(checkIncludes: true);or with expanding specific navigations:
customerQuery.ProjectTo<Customer, CustomerDTO>(c => c.Addresses, c => c.Orders);Note: If you want to change mapping behavior, create a class that inherits from ExpressionProvider, override CreateMemberBinding and inject an instance of your class to MapConfiguration.
First, install NuGet. Then, install BatMap from the package manager console:
PM> Install-Package BatMap
You might want to visit wiki for more.
Developed with ❤️ at Doğuş Teknoloji.