Skip to content

matheushoske/CompactMapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CompactMapper Logo

πŸ“¦ 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

Option 1: NuGet Package (Recommended)

# 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.0" />

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!

About

Simplify .net object mapping with this Mapster and AutoMapper alternative

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages