Mapgen.Analyzer 1.5.0

dotnet add package Mapgen.Analyzer --version 1.5.0
                    
NuGet\Install-Package Mapgen.Analyzer -Version 1.5.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="Mapgen.Analyzer" Version="1.5.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mapgen.Analyzer" Version="1.5.0" />
                    
Directory.Packages.props
<PackageReference Include="Mapgen.Analyzer">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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 Mapgen.Analyzer --version 1.5.0
                    
#r "nuget: Mapgen.Analyzer, 1.5.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 Mapgen.Analyzer@1.5.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=Mapgen.Analyzer&version=1.5.0
                    
Install as a Cake Addin
#tool nuget:?package=Mapgen.Analyzer&version=1.5.0
                    
Install as a Cake Tool

Mapgen

Build And Test NuGet Downloads .NET License

A high-performance, compile-time object mapper for .NET using source generators.

πŸš€ Quick Start

dotnet add package Mapgen.Analyzer
using Mapgen.Analyzer;

// Define your mapper
[Mapper]
public partial class UserMapper
{
    public partial UserDto ToDto(User source);

    public UserMapper()
    {
        MapMember(dto => dto.FullName, 
                  user => $"{user.FirstName} {user.LastName}");
    }
}

// Use it
var userDto = user.ToDto();  // Extension method, auto-generated!

✨ Key Features

  • ⚑ Zero Runtime Overhead - All code generated at compile time
  • πŸ›‘οΈ Type Safe - Full compile-time type checking
  • πŸ” IDE Friendly - Complete IntelliSense, go-to-definition, and debugging support
  • 🎯 Explicit & Clear - No magic, no reflection, readable generated code
  • 🧩 Flexible - Custom mappings, multi-parameter support, collection transformations
  • πŸ“¦ Lightweight - Minimal dependencies
  • πŸ”§ Easy Testing - Test mappers like regular classes

πŸ“š Documentation

Getting Started

Advanced Topics

Migration Guides

🎯 Why Mapgen?

Compile-Time Generation

Unlike runtime mappers (AutoMapper, Mapster), Mapgen generates all mapping code during compilation. This means:

  • Faster execution - No reflection or expression compilation at runtime
  • Earlier error detection - Mapping issues caught during build
  • Better debugging - Step through generated code like any other code

Better Than Reflection-Based Mappers

// Runtime mappers (AutoMapper)
var dto = _mapper.Map<UserDto>(user);  // ❌ Runtime resolution, opaque

// Mapgen
var dto = user.ToDto();  // βœ… Compile-time, transparent

Cleaner Than Attribute-Heavy Approaches

// Attribute-based (Mapperly)
[MapProperty(nameof(User.FirstName) + " + ' ' + " + nameof(User.LastName), 
             nameof(UserDto.FullName))]  // ❌ String-based, verbose

// Mapgen
MapMember(dto => dto.FullName, 
          user => $"{user.FirstName} {user.LastName}");  // βœ… Type-safe, clean

πŸ“– Example Usage

Basic Mapping

[Mapper]
public partial class ProductMapper
{
    public partial ProductDto ToDto(Product source);
}

// Auto-mapped: properties with matching names and compatible types
var productDto = product.ToDto();

Custom Property Mapping

[Mapper]
public partial class OrderMapper
{
    public partial OrderDto ToDto(Order source);

    public OrderMapper()
    {
        MapMember(dto => dto.Total, 
                  order => order.Items.Sum(i => i.Price * i.Quantity));
        
        MapMember(dto => dto.StatusText, GetStatusText);
    }
    
    private static string GetStatusText(Order order) => order.Status switch
    {
        OrderStatus.Pending => "Awaiting Payment",
        OrderStatus.Shipped => "In Transit",
        _ => "Unknown"
    };
}

Multi-Parameter Mapping

[Mapper]
public partial class InvoiceMapper
{
    public partial InvoiceDto ToDto(Invoice invoice, Customer customer);

    public InvoiceMapper()
    {
        MapMember(dto => dto.CustomerName, 
                  (invoice, customer) => customer.Name);
        MapMember(dto => dto.CustomerEmail, 
                  (_, customer) => customer.Email);
    }
}

// Usage
var dto = invoice.ToDto(customer);

Collection Mapping

[Mapper]
public partial class TeamMapper
{
    public partial TeamDto ToDto(Team source);

    public TeamMapper()
    {
        MapCollection<PlayerDto, Player>(
            dest => dest.Players,
            player => player.ToDto()
        );
    }
}

Ignoring Properties

[Mapper]
public partial class UserMapper
{
    public partial UserDto ToDto(User source);

    public UserMapper()
    {
        IgnoreMember(dto => dto.PasswordHash);
        IgnoreMember(dto => dto.SecurityToken);
    }
}

🎨 Real-World Example

// Models
public class Car
{
    public string Make { get; init; }
    public string Model { get; init; }
    public int ReleaseYear { get; init; }
    public CarOwner Owner { get; init; }
}

public class CarDto
{
    public string Make { get; init; }
    public string Model { get; init; }
    public int ReleaseYear { get; init; }
    public string CountryOfOrigin { get; init; }
    public string OwnerFullName { get; init; }
    public DriverDto MainDriver { get; init; }
}

// Mapper
[Mapper]
public partial class CarMapper
{
    public partial CarDto ToCarDto(Car source, Driver driver);

    public CarMapper()
    {
        IncludeMappers([new CarOwnerMapper()]);
        MapMember(dest => dest.CountryOfOrigin, GetCountryName);
        MapMember(dest => dest.MainDriver, 
                  (_, drv) => drv.ToDriverDto());
    }

    private static string GetCountryName(Car src) => src.Make switch
    {
        "Toyota" => "Japan",
        "Ford" => "USA",
        "BMW" => "Germany",
        _ => "Unknown"
    };
}

// Usage
var carDto = car.ToCarDto(driver);

πŸ”„ Migration from Other Libraries

Migrating to Mapgen is straightforward. We provide comprehensive guides:

πŸ†š Comparison

Feature Mapgen AutoMapper Mapster Mapperly
Approach Source Generator Runtime/Compiled Runtime/Compiled Source Generator
Performance ⭐⭐⭐⭐⭐ ⭐⭐⭐ ⭐⭐⭐⭐ ⭐⭐⭐⭐⭐
IDE Support βœ… Full ❌ Limited ❌ Limited βœ… Full
Type Safety βœ… Compile-time ⚠️ Runtime ⚠️ Runtime βœ… Compile-time
Setup ⭐ Simple ⭐⭐⭐⭐ Complex ⭐⭐ Moderate ⭐ Simple
Debugging βœ… Easy ❌ Difficult ⚠️ Moderate βœ… Easy

See the full comparison for details.

πŸ§ͺ Testing

Mappers are easy to test:

[Fact]
public void ToDto_MapsPropertiesCorrectly()
{
    // Arrange
    var user = new User 
    { 
        FirstName = "John", 
        LastName = "Doe" 
    };
    var mapper = new UserMapper();
    
    // Act
    var dto = mapper.ToDto(user);
    
    // Assert
    Assert.Equal("John Doe", dto.FullName);
}

πŸ“¦ Installation

NuGet Package Manager

Install-Package Mapgen.Analyzer

.NET CLI

dotnet add package Mapgen.Analyzer

Package Reference

<ItemGroup>
    <PackageReference Include="Mapgen.Analyzer" Version="1.5.0" 
                      OutputItemType="Analyzer" 
                      ReferenceOutputAssembly="false"/>
</ItemGroup>

πŸ› οΈ Requirements

  • .NET 6.0 or higher
  • C# 10.0 or higher (for source generators)

πŸ“§ Support

If you encounter any issues or have questions, please open an issue on the repository.

⭐ Show Your Support

If you find Mapgen useful, please consider giving it a star! ⭐


Happy Mapping! πŸ—ΊοΈ

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

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.5.0 126 5/2/2026
1.4.0 98 5/1/2026
1.3.0 116 2/27/2026
1.2.0 107 2/25/2026
1.1.0 125 2/4/2026
1.0.1 106 2/4/2026
1.0.0 113 1/29/2026