.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!
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.ExtensionsOr via the .NET CLI:
dotnet add package KutCode.AutoMapper.ExtensionsAll versions can be found here.
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);CreateMap():
IMapFrom<T>create map fromTto implementing classIMapTo<T>create map from implementing class toT
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...
}
}public class SomeDto :
IHaveMap,
IMapWith<SomeEntity>,
IMapTo<AnotherOne>,
IMapFrom<AndAnotherOne>
{
public string Value { get;set; }
public static void Map(Profile profile)
{
// profile.CreateMap...
}
}The library provides several extension methods for registering AutoMapper profiles in your application's dependency injection container.
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);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
);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));
});- Use
IMapWith<T>for reverse mapping - Use
IMapFrom<T>to map fromTto an implementing type - Use
IMapTo<T>to map from an implementing type toT - Use
IHaveMapto customize mapping
If you wanna to buy me a coffee, send any tokens in TON network:
💎 noncommunicado.ton
💎 UQD0zFgp0p-eFnbL4cPA6DYqoeWzGbCA81KuU6BKwdFmf8jv