ForgeSharp.Mapper 1.2.1

dotnet add package ForgeSharp.Mapper --version 1.2.1
                    
NuGet\Install-Package ForgeSharp.Mapper -Version 1.2.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="ForgeSharp.Mapper" Version="1.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ForgeSharp.Mapper" Version="1.2.1" />
                    
Directory.Packages.props
<PackageReference Include="ForgeSharp.Mapper" />
                    
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 ForgeSharp.Mapper --version 1.2.1
                    
#r "nuget: ForgeSharp.Mapper, 1.2.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 ForgeSharp.Mapper@1.2.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=ForgeSharp.Mapper&version=1.2.1
                    
Install as a Cake Addin
#tool nuget:?package=ForgeSharp.Mapper&version=1.2.1
                    
Install as a Cake Tool

ForgeSharp.Mapper

ForgeSharp.Mapper is a high-performance, type-safe, and extensible object mapping library for .NET. It enables you to define, configure, and execute mappings between different object types using a fluent, strongly-typed API. The library is designed for speed, maintainability, and ease of integration with modern .NET applications.


Features

  • Fluent, Type-Safe API:
    Define mappings using expressive, chainable syntax with full compile-time safety.
  • Reflection-Free Runtime Mapping:
    Mappings are compiled to delegates for maximum performance.
  • Extensible Builder Pattern:
    Easily register and configure mappings for any type combination.
  • Validation Tools:
    Built-in utilities to detect unmapped properties and validate mapping completeness.
  • Dependency Injection Ready:
    Seamless integration with Microsoft.Extensions.DependencyInjection with the ForgeSharp.Mapper.DependencyInjection NuGet.
  • Advanced Scenarios:
    Supports custom mapping logic, property transformations, and cloning.

Performance Comparison

At the time of writing, ForgeSharp.Mapper is significantly faster than other popular mapping libraries. Below is a benchmark* comparison against AutoMapper, Mapster, and manual mapping:

Method Mean Error StdDev Ratio RatioSD
Manual 5.321 ns 0.1659 ns 0.3942 ns 1.01 0.10
ForgeSharp.Mapper 11.809 ns 0.2449 ns 0.2291 ns 2.23 0.16
Mapster 21.771 ns 0.4347 ns 0.8977 ns 4.11 0.34
AutoMapper 68.273 ns 1.4161 ns 2.2047 ns 12.90 1.01

Forgesharp.Mapper is:

  • ~46% faster than Mapster
  • ~83% faster than AutoMapper

Despite its simplicity and lightweight footprint, ForgeSharp.Mapper delivers the same core functionality as other major mappers:

  • Complex mapping support
  • Custom value transformations
  • Validation features

*The full benchmark project is included in the repository for reproducibility.


Installation

Add the NuGet package to your project:

dotnet add package ForgeSharp.Mapper

For Dependency Injection support, also install:

dotnet add package ForgeSharp.Mapper.DependencyInjection

Quick Start

1. Define a Mapper Builder

public class MyMapperBuilder : MapperBuilder
{
    public MyMapperBuilder()
    {
        Register<Source, Destination>()
            .To(d => d.Name).From(s => s.SourceName)
            .To(d => d.Age).From((s, d) => s.Years + 1);
    }
}

2. Register the Mapper in DI

services.AddMapper<MyMapperBuilder>();

3. Use the Mapper

var mapper = serviceProvider.GetRequiredService<IMapperService>();
var destination = mapper.Map<Source, Destination>(sourceObject);

Advanced Usage

The Configure extension method allows you to define complex mappings in a single, expressive member-initializer expression. This makes your mapping configuration more readable and maintainable, especially for larger objects.

Register<Source, Destination>()
    .Configure((src, dest) => new Destination
    {
        Name = src.SourceName,
        Age = src.Years + 1,
        IsActive = true
    });

You can also use Configure for context-aware mappings:

RegisterWithContext<Source, MyContext, Destination>()
    .Configure((src, ctx, dest) => new Destination
    {
        Name = src.SourceName,
        UserId = ctx.CurrentUser,
        IsActive = true
    });

This just uses reflection to handle the fluent API, but the actual mapping is still compiled to delegates for performance.

Cloning

Register<MyType, MyType>().UseReflectionToClone();

Validation

var tester = serviceProvider.GetRequiredService<IMapperTester>();
foreach (var result in tester.ValidateMissingProperties())
{
    Console.WriteLine(result);
}

Custom Reflection-Based Mapping

register.AddAssignment(
    typeof(Destination).GetProperty(nameof(Destination.SomeProperty)),
    (Expression<Func<Source, string>>)(s => s.SomeSourceProperty)
);

Context-aware Mapping

ForgeSharpMapper supports context-aware mappers, allowing you to inject a custom context object into the mapping logic.

public class MyContext(Guid currentUser)
{
    public Guid CurrentUser { get; set; } = currentUser;
}

This context can then be injected into the mapping logic:

RegisterWithContext<Source, MyContext, Destination>()
    .To(d => d.UserId).From((s, ctx) => ctx.CurrentUser)
    .To(d => d.Name).From(s => s.SourceName);

API Overview

  • IMapperService: Main entry point for mapping operations.
  • MapperBuilder: Base class for registering and configuring mappings.
  • IMapperRegister: Fluent interface for mapping configuration.
  • IMapperTester: Utility for validating mapping completeness.
  • ReflectionExtension / ConfigureExtension / CloneExtension: Helpers for advanced and reflection-based mapping scenarios.

Project Structure

  • MapperRegistry.cs – Core fluent mapping API and registry.
  • MapperService.cs – Service and DI integration.
  • MapperTester.cs – Validation and diagnostics.
  • Reflection/ – Extensions for reflection-based and advanced mapping.

Requirements

  • .NET 8.0 or later

Contributing

Contributions are welcome! Please open issues or submit pull requests for bug fixes, improvements, or new features.


License

MIT License


Acknowledgements

  • Inspired by AutoMapper and other mapping libraries, but designed for maximum performance, type safety and zero reflection.
  • Built with modern C# features and best practices.

Contact

For questions, suggestions, or support, please open an issue on GitHub.


ForgeSharp.Mapper – Fast, type-safe, and extensible mapping for .NET.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ForgeSharp.Mapper:

Package Downloads
ForgeSharp.Mapper.DependencyInjection

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.1 104 7/10/2025
1.2.0 100 7/9/2025
1.1.0 133 7/2/2025
1.0.0 84 6/28/2025