KutCode.AutoMapper.Extensions 1.0.0

dotnet add package KutCode.AutoMapper.Extensions --version 1.0.0                
NuGet\Install-Package KutCode.AutoMapper.Extensions -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="KutCode.AutoMapper.Extensions" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add KutCode.AutoMapper.Extensions --version 1.0.0                
#r "nuget: KutCode.AutoMapper.Extensions, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install KutCode.AutoMapper.Extensions as a Cake Addin
#addin nuget:?package=KutCode.AutoMapper.Extensions&version=1.0.0

// Install KutCode.AutoMapper.Extensions as a Cake Tool
#tool nuget:?package=KutCode.AutoMapper.Extensions&version=1.0.0                

KutCode.AutoMapper.Extensions

.NET library that allows you:

  • Configure Mappings in the type definition
  • Use inheritance of interfaces for "default" mappings, without complex rules

📜 Installation

KutCode.AutoMapper.Extensions is designed with net7.0, net8.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() if not overrided:

  • IMapFrom<T> create map from T to implementing class
  • IMapTo<T> create map from implementing class to T

Override default mapping

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")
            );
    }
}

Use multiple interfaces

public class SomeDto : IMapWith<SomeEntity>,
    IMapTo<AnotherOne>, IMapFrom<AndAnotherOne>
{
    public string Value { get;set; }
    
    // also overrides available for all intrfaces
}

✨ Conclusion

  • Use IMapWith<T> for reverse mapping
  • Use IMapFrom<T> to map from T to an implementing type
  • Use IMapTo<T> to map from an implementing type to T

All of these interfaces allow you to override the Map(MapProfileDecorator<T>) method to customize the mapping.

☕ Contribution

If you wanna to buy me a coffee, send any tokens in TON network:
💎 noncommunicado.ton
💎 UQD0zFgp0p-eFnbL4cPA6DYqoeWzGbCA81KuU6BKwdFmf8jv

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 101 7/31/2024