.NET library that allows you:
- Configure Mappings in the type definition
- Use inheritance of interfaces for "default" mappings, without complex rules
KutCode.AutoMapper.Extensions
is designed with net7.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.
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()
if not overrided:
IMapFrom<T>
create map fromT
to implementing classIMapTo<T>
create map from implementing class toT
If you just inherite interface IMapWith<T>
- that will created .ReverseMap()
for two types.
So, you can override default mapping, and set your own behaviour:
public class SomeDto : IMapWith<SomeEntity>
{
public string Value { get;set; }
// override Map method
public void Map(MapProfileDecorator<SomeEntity> decorator)
{
decorator.Profile.CreateMap<DataDto, DataEntity>()
.ForMember(m => m.Value, opt
=> opt.MapFrom(f => "SomeOverride")
);
}
}
public class SomeDto : IMapWith<SomeEntity>,
IMapTo<AnotherOne>, IMapFrom<AndAnotherOne>
{
public string Value { get;set; }
// also overrides available for all intrfaces
}
- 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
All of these interfaces allow you to override the Map(MapProfileDecorator<T>)
method to customize the mapping.
If you wanna to buy me a coffee, send any tokens in TON network:
💎 noncommunicado.ton
💎 UQD0zFgp0p-eFnbL4cPA6DYqoeWzGbCA81KuU6BKwdFmf8jv