Azka.ObjectMapper 10.0.0-rc.1

This is a prerelease version of Azka.ObjectMapper.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Azka.ObjectMapper --version 10.0.0-rc.1
                    
NuGet\Install-Package Azka.ObjectMapper -Version 10.0.0-rc.1
                    
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="Azka.ObjectMapper" Version="10.0.0-rc.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Azka.ObjectMapper" Version="10.0.0-rc.1" />
                    
Directory.Packages.props
<PackageReference Include="Azka.ObjectMapper" />
                    
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 Azka.ObjectMapper --version 10.0.0-rc.1
                    
#r "nuget: Azka.ObjectMapper, 10.0.0-rc.1"
                    
#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 Azka.ObjectMapper@10.0.0-rc.1
                    
#: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=Azka.ObjectMapper&version=10.0.0-rc.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Azka.ObjectMapper&version=10.0.0-rc.1&prerelease
                    
Install as a Cake Tool

Azka.ObjectMapper

NuGet .NET

Azka.ObjectMapper is a lightweight object mapping library for .NET, designed to map data between classes (for example, Entity to DTO) using profile-based configuration.

Features

  • Object mapping TSource -> TDestination.
  • Collection mapping IEnumerable<TSource> -> IEnumerable<TDestination>.
  • Mapping into an existing destination instance.
  • Custom member mapping with ForMember(...).
  • Ignore source members with Ignore(...).
  • Nested object mapping support (when profiles exist).
  • Nested collection mapping support.
  • Simple type conversion support for primitives, nullable values, and enums.
  • Mutable read-only collection population support.
  • Integration with Microsoft.Extensions.DependencyInjection.

Installation

dotnet add package Azka.ObjectMapper

Quick Start

1. Create models

public class User
{
    public Guid Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public string Email { get; set; } = string.Empty;
    public DateTime CreatedAt { get; set; }
}

public class UserDto
{
    public Guid Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public string Email { get; set; } = string.Empty;
    public long CreatedAtUnix { get; set; }
}

2. Create a mapping profile

using Azka.ObjectMapper;

public class UserMapProfile : ObjectMapProfile
{
    public UserMapProfile()
    {
        CreateMap<User, UserDto>()
            .ForMember(dest => dest.CreatedAtUnix, src =>
                new DateTimeOffset(src.CreatedAt).ToUnixTimeSeconds());
    }
}

3. Register in DI

Option A - register a profile instance:

using Azka.ObjectMapper;

builder.Services.AddObjectMapper(new UserMapProfile());

Option B - scan profiles from assembly:

using System.Reflection;
using Azka.ObjectMapper;

builder.Services.AddObjectMapper(Assembly.GetExecutingAssembly());

Option C - scan profiles from multiple assemblies:

using System.Reflection;
using Azka.ObjectMapper;

builder.Services.AddObjectMapper(new List<Assembly>
{
    Assembly.GetExecutingAssembly(),
    typeof(SharedMapProfile).Assembly
});

Option D - use without DI:

using Azka.ObjectMapper;

IObjectMapper mapper = new DefaultObjectMapper(new UserMapProfile());

4. Use in service/controller

using Azka.ObjectMapper;

public class UsersService
{
    private readonly IObjectMapper _mapper;

    public UsersService(IObjectMapper mapper)
    {
        _mapper = mapper;
    }

    public UserDto GetDto(User user)
    {
        return _mapper.Map<User, UserDto>(user);
    }

    public IEnumerable<UserDto> GetDtos(IEnumerable<User> users)
    {
        return _mapper.Map<User, UserDto>(users);
    }

    public UserDto UpdateDto(User user, UserDto dto)
    {
        return _mapper.Map<User, UserDto>(user, dto);
    }
}

Mapping Configuration

ForMember (custom destination member)

CreateMap<User, UserDto>()
    .ForMember(dest => dest.Name, src => $"[USER] {src.Name}");

Ignore (ignore source member)

CreateMap<User, UserDto>()
    .Ignore(src => src.Email);

Type conversion

When source and destination property names match, the mapper attempts compatible simple conversions such as string to int, numeric values to enums, enum names from strings, and nullable destination values.

public class User
{
    public string Age { get; set; } = "20";
    public string Status { get; set; } = "Active";
}

public class UserDto
{
    public int Age { get; set; }
    public UserStatus Status { get; set; }
}

public enum UserStatus
{
    Active,
    Inactive
}

Nested Mapping

If a property is a complex object or a collection of complex objects, profiles for child types must also be available.

public class Address
{
    public string City { get; set; } = string.Empty;
}

public class AddressDto
{
    public string City { get; set; } = string.Empty;
}

public class User
{
    public Address Address { get; set; } = new();
}

public class UserDto
{
    public AddressDto Address { get; set; } = new();
}

public class UserMapProfile : ObjectMapProfile
{
    public UserMapProfile()
    {
        CreateMap<Address, AddressDto>();
        CreateMap<User, UserDto>();
    }
}

Collection Mapping

Collections are mapped by element type. Primitive collections can be converted when the element values are compatible, and complex object collections require child profiles.

public class User
{
    public string[] Tags { get; set; } = [];
}

public class UserDto
{
    public List<string> Tags { get; set; } = [];
}

Collection values are created internally as List<TDestinationElement>. This works well for destination properties typed as List<T>, IEnumerable<T>, ICollection<T>, or other compatible assignable collection abstractions. Destination array properties such as string[] are not converted from that internal list automatically.

For destination properties without a public setter, the mapper can populate the existing collection when it is mutable, such as a List<T> exposed through a read-only property.

public class UserDto
{
    public List<string> Tags { get; } = [];
}

When populating an existing mutable collection, the mapper clears the current collection and adds the mapped items.

API Summary

  • IObjectMapper.Map<T, U>(T obj) maps a single object to destination type.
  • IObjectMapper.Map<T, U>(IEnumerable<T> listOfObjects) maps an object collection to destination collection type.
  • IObjectMapper.Map<T, U>(T source, U target) maps source values into an existing destination instance and returns that same destination instance.
  • MapProfile<T, U>.ForMember(...) defines custom mapping for a destination member.
  • MapProfile<T, U>.Ignore(...) ignores a source member.
  • IServiceCollection.AddObjectMapper(ObjectMapProfile profile) registers a mapper from a profile instance.
  • IServiceCollection.AddObjectMapper(Assembly assembly) scans public profile classes from an assembly.
  • IServiceCollection.AddObjectMapper(List<Assembly> assemblies) scans public profile classes from multiple assemblies.
  • DefaultObjectMapper(ObjectMapProfile profile) creates a mapper directly from one profile without DI.
  • DefaultObjectMapper(IEnumerable<ObjectMapProfile> profiles) creates a mapper directly from multiple profiles without DI.

Important Notes

  • Mapping to a new destination instance requires the destination type to have a public parameterless constructor.
  • Mapping into an existing destination instance does not create a new destination instance, so the destination type itself does not need a parameterless constructor for that API.
  • Mapping into an existing destination instance requires both source and target arguments to be non-null.
  • Mapping only works when a CreateMap<T, U>() profile exists.
  • For nested types, nested profiles are also required.
  • Nested destination types that must be created by the mapper require a public parameterless constructor.
  • Collection item destination types that must be created by the mapper require a public parameterless constructor.
  • If a mapping profile is not found, mapper throws InvalidOperationException.
  • If source is null when mapping to a new destination, mapper returns default.
  • If source is null when mapping into an existing destination instance, mapper throws ArgumentException.
  • If a non-custom property value cannot be converted to the destination property type, that property is skipped.
  • If a custom ForMember(...) value cannot be converted to the destination property type, mapper throws InvalidOperationException.
  • Assembly scanning only registers public, non-abstract ObjectMapProfile classes with a parameterless constructor.
  • Assembly scanning includes public nested profile classes and ignores ObjectMapProfileCollections.
  • Combining multiple profiles with the same source and destination pair throws DuplicateWaitObjectException.

Requirements

License

See LICENSE.txt.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.

Version Downloads Last Updated
10.0.0-rc.4 50 11/9/2026
10.0.0-rc.3 70 11/7/2026
10.0.0-rc.2 67 10/27/2026
10.0.0-rc.1 116 6/30/2026
10.0.0-alpha6 92 6/20/2026
10.0.0-alpha5 102 3/16/2026
10.0.0-alpha.9 91 6/25/2026
10.0.0-alpha.8 90 6/25/2026
10.0.0-alpha.7 99 6/25/2026

Initial release of Azka.ObjectMapper
     - Object Mapper