Najlot.Map.SourceGenerator 0.1.1

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

M Najlot.Map

Najlot.Map is a lightweight, high-performance object-to-object mapping library for .NET. It combines the simplicity of a central mapping container with the performance of handwritten mapping code.

The library is designed to:

  • avoid reflection at runtime,
  • keep mappings explicit and readable,
  • scale from handwritten mappings to compile-time generated mappings.

Features

  • Explicit, method-based mappings
  • Central IMap container
  • Zero magic at runtime
  • Optional Roslyn Source Generator
  • Mapping validation for test environments
  • Collection and nested object mapping
  • Custom converters
  • Dependency injection support

Packages

Najlot.Map is distributed as two NuGet packages:

Package Description
Najlot.Map Core mapping infrastructure
Najlot.Map.SourceGenerator Compile-time mapping generation

Installation

Core library

dotnet add package Najlot.Map

Source generator (optional)

dotnet add package Najlot.Map.SourceGenerator

Core Concepts

  • Mappings are methods, not configuration.
  • Only public methods are registered.
  • IMap is the single entry point for all mappings.
  • Mappings can be validated during testing.

Manual Mapping Example

You can write mapping methods manually without the source generator.

internal class UserMapMethods
{
    [MapIgnoreProperty(nameof(User.Password))]
    public void MapModelToUser(IMap map, UserModel from, User to)
    {
        to.Id = from.Id;
        to.Username = from.Username;
        to.Features = map
            .From<UserFeatureModel>(from.Features)
            .ToList<UserFeature>();
    }

    [MapIgnoreProperty(nameof(UserModel.Password))]
    public UserModel MapUserToModel(IMap map, User from) => new()
    {
        Id = from.Id,
        Username = from.Username,
        Features = map
            .From<UserFeature>(from.Features)
            .To<UserFeatureModel>()
    };

    [MapIgnoreMethod]
    public Guid IgnoredMethod(UserModel from) => from.Id;
}

Registering Mappings

Manual Registration

var map = new Map()
    .Register<UserMapMethods>()
    .RegisterFactory(type => IocContainer.Resolve(type));

Auto-Registration (Source Generator)

The source generator creates an extension method Register{AssemblyName}Mappings() that registers all classes marked with [Mapping].

var map = new Map()
    .RegisterMyProjectMappings() // Assuming assembly name is "MyProject"
    .RegisterFactory(type => IocContainer.Resolve(type));

Mapping Objects

var model = map.From(user).To<UserModel>();

Mapping Collections

var models = map.From<User>(users).To<UserModel>();

Mapping Validation (Tests Only)

Validate() compares destination properties with used mappings and throws when mappings are incomplete.

map.Validate();

⚠️ Recommended only in unit tests, not in production.


Source Generator

The source generator creates mapping implementations at compile time.

Example

using Najlot.Map;
using Najlot.Map.Attributes;

[Mapping]
public partial class UserMappings
{
    public partial void MapUser(IMap map, UserModel from, User to);
    public partial void MapFeature(IMap map, FeatureModel from, Feature to);

    private partial void PartialMapAddress(
        IMap map,
        AddressModel from,
        AddressViewModel to);

    public void MapAddress(
        IMap map,
        AddressModel from,
        AddressViewModel to)
    {
        // Custom logic before
        PartialMapAddress(map, from, to);
        // Custom logic after
    }

    // Custom converter (auto-detected)
    public DateTime ConvertToUtc(DateTimeOffset offset)
        => offset.UtcDateTime;
}

Usage

var map = new Map()
    // Registers all [Mapping] classes in the assembly
    .RegisterMyProjectMappings() 
    .RegisterFactory(type => /* your factory */);

var user = map.From(userModel).To<User>();

When to Use the Source Generator

Use it if you want:

  • maximum performance
  • zero runtime overhead
  • compile-time safety
  • reduced boilerplate

Manual mappings remain fully supported and interoperable.


License

MIT License


There are no supported framework assets in this 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
0.2.1 78 4/14/2026
0.2.0 87 3/30/2026
0.1.2 242 12/23/2025
0.1.1 183 12/22/2025
0.1.0 129 12/20/2025
0.0.8 280 12/18/2025
0.0.7 225 12/14/2025
0.0.6 158 12/13/2025