KutCode.AutoMapper.Extensions
2.1.0
dotnet add package KutCode.AutoMapper.Extensions --version 2.1.0
NuGet\Install-Package KutCode.AutoMapper.Extensions -Version 2.1.0
<PackageReference Include="KutCode.AutoMapper.Extensions" Version="2.1.0" />
<PackageVersion Include="KutCode.AutoMapper.Extensions" Version="2.1.0" />
<PackageReference Include="KutCode.AutoMapper.Extensions" />
paket add KutCode.AutoMapper.Extensions --version 2.1.0
#r "nuget: KutCode.AutoMapper.Extensions, 2.1.0"
#:package KutCode.AutoMapper.Extensions@2.1.0
#addin nuget:?package=KutCode.AutoMapper.Extensions&version=2.1.0
#tool nuget:?package=KutCode.AutoMapper.Extensions&version=2.1.0
<img src="./img/icon.png" style="width: 40px" /> KutCode.AutoMapper.Extensions
<img src="https://img.shields.io/github/v/tag/noncommunicado/KutCode.AutoMapper.Extensions?include_prereleases&style=flat&label=Version&color=darkgreen" />
.NET library that allows you to:
✅ Configure Mappings in the type declaration
✅ Use inheritance of interfaces for "default" mappings, without complex rules
✅ Create custom mapping with Profile
in type declaration!
📖 Table of Contents
📜 Installation
KutCode.AutoMapper.Extensions
is designed for net7.0
, net8.0
, net9.0
and higher.
Install KutCode.AutoMapper.Extensions
using NuGet Package Manager:
Install-Package KutCode.AutoMapper.Extensions
Or via the .NET CLI:
dotnet add package KutCode.AutoMapper.Extensions
All versions can be found here.
🚀 Quick Start
Basic example
Let's declare two types:
public class SomeEntity
{
public string Value { get;set; }
}
public class SomeDto : IMapWith<SomeEntity> // <-- just inherit it
{
public string Value { get;set; }
}
Use DI to configure AutoMapper:
global using AutoMapper;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// just call this to scan All assemblies
builder.Services.AddAllMappings();
// or select assemblies manually
builder.Services.AddMappings(typeof(Program).Assembly, typeof(Domain).Assembly);
So, that's all, now you can map with AutoMapper's IMapper
as usual:
SomeDto dto = mapper.Map<SomeDto>(entity);
IMapFrom<T>
and IMapTo<T>
⚠️ Whereas, you can also use those interfaces, which just calls CreateMap()
:
IMapFrom<T>
create map fromT
to implementing classIMapTo<T>
create map from implementing class toT
Override default mapping
So, you can override default mapping, just inherit interface IHaveMap
.
And set your own behaviour right in type definition:
public class SomeDto : IHaveMap // ✅ Inherit interface
{
public string Value { get;set; }
// ✅ Implement Map method
public static void Map(Profile profile)
{
profile.CreateMap<DataDto, DataEntity>()
.ForMember(m => m.Value, opt
=> opt.MapFrom(f => "SomeOverride")
);
// any other mappings...
}
}
Use multiple interfaces
public class SomeDto :
IHaveMap,
IMapWith<SomeEntity>,
IMapTo<AnotherOne>,
IMapFrom<AndAnotherOne>
{
public string Value { get;set; }
public static void Map(Profile profile)
{
// profile.CreateMap...
}
}
💉 Dependency Injection
The library provides several extension methods for registering AutoMapper profiles in your application's dependency injection container.
Register All Mappings
To scan and register all mappings from all loaded assemblies:
// Register all mappings from all loaded assemblies
builder.Services.AddAllMappings();
// With custom configuration
builder.Services.AddAllMappings(cfg => {
// Custom configurations here
cfg.AllowNullDestinationValues = true;
});
// Including default AutoMapper profiles
builder.Services.AddAllMappings(catchDefaultProfiles: true);
Register Specific Assemblies
To scan only specific assemblies for mappings:
// Register mappings from specific assemblies
builder.Services.AddMappings(
typeof(Program).Assembly,
typeof(Domain).Assembly
);
// With custom configuration
builder.Services.AddMappings(
cfg => {
cfg.CreateMap<CustomSource, CustomDestination>();
},
catchDefaultProfiles: true,
typeof(Program).Assembly
);
Custom Configuration
For explicit AutoMapper configuration:
// Register AutoMapper with explicit configuration
builder.Services.AddMappings(cfg => {
cfg.CreateMap<Source, Destination>()
.ForMember(dest => dest.Property, opt => opt.MapFrom(src => src.OtherProperty));
});
✨ Conclusion
- Use
IMapWith<T>
for reverse mapping - Use
IMapFrom<T>
to map fromT
to an implementing type - Use
IMapTo<T>
to map from an implementing type toT
- Use
IHaveMap
to customize mapping
☕ Contribution
If you wanna to buy me a coffee, send any tokens in TON network:
💎 noncommunicado.ton
💎 UQD0zFgp0p-eFnbL4cPA6DYqoeWzGbCA81KuU6BKwdFmf8jv
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net7.0
- AutoMapper (>= 13.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
-
net8.0
- AutoMapper (>= 14.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
-
net9.0
- AutoMapper (>= 14.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.