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
                    
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="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="KutCode.AutoMapper.Extensions" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="KutCode.AutoMapper.Extensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add KutCode.AutoMapper.Extensions --version 2.1.0
                    
#r "nuget: KutCode.AutoMapper.Extensions, 2.1.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.
#:package KutCode.AutoMapper.Extensions@2.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=KutCode.AutoMapper.Extensions&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=KutCode.AutoMapper.Extensions&version=2.1.0
                    
Install as a Cake Tool

<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 from T to implementing class
  • IMapTo<T> create map from implementing class to T

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 from T to an implementing type
  • Use IMapTo<T> to map from an implementing type to T
  • 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 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. 
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.