CompactMapper 1.0.1

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

<div align="center"> <img src="logo.png" alt="CompactMapper Logo" width="200"> </div>

📦 CompactMapper

CompactMapper is a lightweight, fast, and fully open-source object mapper for .NET that lets you map objects between types — including complex types and collections — with just one line of code.

🟢 No dependencies. No profiles. No configuration. Just pure mapping.
🔄 An open-source alternative to AutoMapper, which is now commercially licensed.

Created by Hoskes


🚀 Why CompactMapper?

AutoMapper is an industry-standard library for object mapping in .NET — but starting from version 12, AutoMapper is no longer free for commercial use under its new license.

CompactMapper is:

  • ✅ Free and open-source
  • ✅ Simple and extensible
  • ✅ Handles nested mapping and collections
  • ✅ Ideal for DTO mapping, clean architecture, and layering concerns

✨ Features

  • 🔄 Automatic mapping of properties by name (case-insensitive)
  • 🧠 Recursive mapping of nested objects
  • 📚 Collection and array mapping
  • 🎯 Optional custom mappings using lambdas
  • 🧰 Optional value transformers (e.g., trimming, formatting, sanitizing)
  • 💡 No reflection after initial setup — highly efficient

📦 Installation

# Using Package Manager Console
Install-Package CompactMapper

# Using .NET CLI
dotnet add package CompactMapper

# Using PackageReference in .csproj file
<PackageReference Include="CompactMapper" Version="1.0.1" />

After installing, add the namespace in your code files:

using CompactMapper;

Option 2: Manual Installation

Copy the CompactMapperExtension class into your project.


🧑‍💻 Usage

🔹 1. Basic Mapping

var customerDto = customer.MapTo<CustomerDto>();

🔹 2. Mapping with Nested Objects

var dto = order.MapTo<OrderDto>(); // Will also map order.Customer, order.Items, etc.

🔹 3. Mapping Collections

List<CustomerDto> customerDtos = customers.MapTo<List<CustomerDto>>();

Works for List<T>, IEnumerable<T>, T[], etc.

🔹 4. Custom Mapping Logic

CompactMapperExtension.AddCustomMapping<Customer, CustomerDto>((src, dest) =>
{
    dest.FullName = $"{src.FirstName} {src.LastName}";
});

🔹 5. Value Transformation (Optional)

var dto = user.MapTo<UserDto>(valueTransformer: (prop, value) =>
{
    if (prop == "Email" && value is string email)
        return email.ToLowerInvariant();

    return value;
});

📐 Example

🧱 Entities

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Address Address { get; set; }
    public List<Order> Orders { get; set; }
}

public class Address
{
    public string Street { get; set; }
}

📦 DTOs

public class CustomerDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public AddressDto Address { get; set; }
    public List<OrderDto> Orders { get; set; }
}

public class AddressDto
{
    public string Street { get; set; }
}

✅ Mapping

var customerDto = customer.MapTo<CustomerDto>();

No config, no fuss — deeply nested and collections mapped out of the box!


💬 Why Not AutoMapper?

Feature AutoMapper CompactMapper
Free for commercial use ❌ (v12+) ✅ Always
Configuration required ✅ Yes ❌ No
Profiles and Setup ✅ Required ❌ Not Needed
Collection Support ✅ Yes ✅ Yes
Nested Object Mapping ✅ Yes ✅ Yes
Custom Actions ✅ Yes ✅ Yes
Lightweight ❌ Heavy at times ✅ One class only

🛠️ Under the Hood

  • Uses reflection only at runtime, no compilation step
  • Recursive mapping using MethodInfo.MakeGenericMethod
  • Collection type detection via IEnumerable<> interfaces
  • Internal registry of custom mappings per type pair

📣 Roadmap

  • NuGet Package: CompactMapper
  • Fluent API style (optional config)
  • Support for flattening/nested property paths
  • Dictionary mapping (Dictionary<string, object> ↔ POCO)

📝 License

MIT License – free to use for personal and commercial projects.


🙌 Contributing

Want to help improve CompactMapper? PRs are welcome!
If you'd like to add features or extensions (like flattening or reverse mapping), feel free to fork and contribute.


📫 Contact

Feel free to open an issue or reach out if you use CompactMapper in your project — we'd love to hear how it's helping!

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 was computed.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.1

    • 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.0.1 186 4/18/2025
1.0.0 170 4/16/2025