NeatMapper 5.0.0
dotnet add package NeatMapper --version 5.0.0
NuGet\Install-Package NeatMapper -Version 5.0.0
<PackageReference Include="NeatMapper" Version="5.0.0" />
paket add NeatMapper --version 5.0.0
#r "nuget: NeatMapper, 5.0.0"
// Install NeatMapper as a Cake Addin #addin nuget:?package=NeatMapper&version=5.0.0 // Install NeatMapper as a Cake Tool #tool nuget:?package=NeatMapper&version=5.0.0
.NEaT Mapper
What is this package
.NEaT Mapper is a .NET object mapper (and now projector too!), with configurable and reusable mappings.
This allows you to create mappings (and projections) between different types (even generic ones), combine them, nest them and reuse them, making your code DRY. Map once, use everywhere.
All of this is achieved with explicit strongly typed maps, easily debuggable, no implicit mappings, no conventions, no compilation into obscure expressions, no black boxes.
It also supports asynchronous maps and services via Dependency Injection (DI).
How to install
You can install this package directly from Nuget https://www.nuget.org/packages/NeatMapper
How to use
1. Create mapping classes
You have to create one or more classes implementing one of the following mapping interfaces:
INewMap<TSource, TDestination>
to map existing objects to new onesIMergeMap<TSource, TDestination>
to map and merge an object with another oneIProjectionMap<TSource, TDestination>
to project an object into another one, this will create an expression which can be used with LINQ or compiled into a NewMap
You can also create async
maps by implementing the interfaces:
IAsyncNewMap<TSource, TDestination>
IAsyncMergeMap<TSource, TDestination>
If you are on .NET 7 or greater you can use the static
versions of the interfaces above: INewMapStatic<TSource, TDestination>
, IMergeMapStatic<TSource, TDestination>
, IProjectionMapStatic<TSource, TDestination>
, IAsyncNewMapStatic<TSource, TDestination>
, IAsyncMergeMapStatic<TSource, TDestination>
.
If you create a class with more than 1 mapping of the same interface you must implement them explicitly like below.
public class MyMaps :
INewMap<Product, ProductDto>,
IAsyncMergeMap<Category, CategoryDto>,
IProjectionMap<Book, BookDto>
{
ProductDto? INewMap<Product, ProductDto>.Map(Product? source, MappingContext context){
if(source == null)
return null;
else{
return new ProductDto{
Code = source.Code,
...
};
}
}
async Task<CategoryDto?> IAsyncMergeMap<Category, CategoryDto>.MapAsync(Category? source, CategoryDto? destination, AsyncMappingContext context){
if(source != null){
destination ??= new CategoryDto();
destination.Id = source.Id;
destination.Parent = await context.Mapper.MapAsync(source.Parent, destination.Parent, context.CancellationToken);
...
}
return destination;
}
Expression<Func<Book, BookDto>> IProjectionMap<Book, BookDto>.Project(ProjectionContext context){
return source => source == null ? null : new BookDto{ ... };
}
}
2. Configure the services
The easiest way to create a mapper is to use Dependency Injection (DI), which will handle all the configuration for you.
// Add all available maps
services.Configure<CustomMapsOptions>(o => o.TypesToScan = Assembly.GetExecutingAssembly().GetTypes().ToList() );
// Or add specific maps
//services.Configure<CustomMapsOptions>(o => o.TypesToScan = new List<Type>{ typeof(MyMaps), ... });
services.AddNeatMapper();
...
IMapper mapper = serviceProvider.GetRequiredService<IMapper>();
IAsyncMapper asyncMapper = serviceProvider.GetRequiredService<IAsyncMapper>();
IProjector projector = serviceProvider.GetRequiredService<IProjector>();
3. Map your types
Map your types by invoking the generic methods available.
Note that mapping matches types exactly, so parent or derived classes won't work.
// Create a new object
var myProductDto = mapper.Map<ProductDto>(myProduct);
// Map to an existing object asynchronously (the types are auto-inferred)
await asyncMapper.MapAsync(myCategory, myCategoryDto);
// Create a projection to use in a LINQ query
var myBookDtos = db.Set<Book>()
.Project<BookDto>(projector)
.ToArray();
Advanced options
Find more advanced use cases in the website or in the extended tests project.
License
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.1 is compatible. |
.NET Framework | net47 is compatible. net471 was computed. net472 was computed. net48 is compatible. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 3.1
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.1.0 && < 4.0.0)
- Microsoft.Extensions.Options (>= 3.1.0 && < 4.0.0)
-
.NETFramework 4.7
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0 && < 8.0.0)
- Microsoft.Extensions.Options (>= 7.0.0 && < 8.0.0)
-
.NETFramework 4.8
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0 && < 8.0.0)
- Microsoft.Extensions.Options (>= 7.0.0 && < 8.0.0)
-
.NETStandard 2.1
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0 && < 8.0.0)
- Microsoft.Extensions.Options (>= 7.0.0 && < 8.0.0)
-
net5.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 5.0.0 && < 6.0.0)
- Microsoft.Extensions.Options (>= 5.0.0 && < 6.0.0)
-
net6.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0 && < 8.0.0)
- Microsoft.Extensions.Options (>= 7.0.0 && < 8.0.0)
-
net7.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0 && < 8.0.0)
- Microsoft.Extensions.Options (>= 7.0.0 && < 8.0.0)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0 && < 9.0.0)
- Microsoft.Extensions.Options (>= 8.0.0 && < 9.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on NeatMapper:
Package | Downloads |
---|---|
NeatMapper.EntityFrameworkCore
Creates automatic maps and projections between entities and their keys (even composite and shadow keys), supports normal maps and asynchronous ones, also supports collections (not nested). |
|
NeatMapper.Transitive
Allows mapping types by automatically chaining maps together, eg: If you have maps for types A -> B and B -> C you can also map A -> C by chaining A -> B -> C, supports normal maps and asynchronous ones, also supports collections. |
GitHub repositories
This package is not used by any popular GitHub repositories.
See full changelog at https://www.neatmapper.org/changelog#500---2024-11-03