Skip to content
manusoftPublic

About

Mappy is a lightweight object mapping utility for C# applications.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

39 Commits

Folders and files

Repository files navigation

Static Badge NuGet Version NuGet Downloads License

🍁Mappy - Object Mapping

8253945

Mappy is a lightweight, convention-first object mapper for .NET with zero external runtime dependencies.

It keeps the common case simple:

var dto = entity.Map<EntityDto>();

Mappy focuses on predictable mapping, cached mapping plans, compiled property accessors, nested objects, collections, positional records and immutable constructor types, circular and shared-reference tracking, explicit converters, and in-place updates.

Why Mappy?

  • Zero mandatory configuration—convention mapping works out of the box
  • Cached mapping plans per source, destination, and member visibility combination
  • Compiled property getters and setters
  • Nested object and collection/array mapping (List<T>, HashSet<T>, arrays, and compatible collection interfaces)
  • Constructor and positional record mapping, including unmatched/extra parameters
  • Circular and shared-reference preservation across graphs and collections
  • Strongly typed and string-based property rename and ignore rules
  • Explicit type converters plus enum and compatible numeric conversions
  • MapTo for updating existing instances and customMapping / with for post-map enrichment
  • Detailed MappingException diagnostics (SourceType, DestinationType, MemberName)
  • Targets netstandard2.0 and net10.0 with no external runtime dependencies

Installation

dotnet add package Mappy.dotNet --version 4.2.0

Basic Mapping

Properties with matching names (case-insensitive) and compatible types are mapped automatically:

var source = new User
{
    Id = 1,
    Name = "Manoj",
    Email = "manoj@example.com"
};

var dto = source.Map<UserDto>();

Nested Mapping

Mappy recursively maps compatible nested objects:

Order
 └── Customer
      ↓
OrderDto
 └── CustomerDto

var dto = order.Map<OrderDto>();

Collections

Map sequences into List<T>, arrays, HashSet<T>, or compatible collection interfaces:

var result = users.MapCollection<UserDto>();
UserDto[] array = users.MapCollection<UserDto>().ToArray();

In Mappy 4.2, MapCollection and MapCollectionAsync share a single mapping context across items by default so shared references across elements preserve their mapped identity (handleCircularReferences: false disables this if needed).

Renaming and Ignoring Properties

Use strongly typed expressions when property names differ:

var config = new MappingConfiguration()
    .Map<User, UserDto>(
        source => source.FullName,
        destination => destination.Name)
    .Ignore<User, UserDto>(source => source.PasswordHash);

var dto = user.Map<UserDto>(config: config);

String-based configuration methods are also supported:

var config = new MappingConfiguration();
config.AddPropertyMapping(typeof(User), typeof(UserDto), "FullName", "Name");
config.ExcludeProperty(typeof(User), typeof(UserDto), "PasswordHash");

Explicit Converters and Conversions

Register custom type converters when you need explicit transformation logic:

var config = new MappingConfiguration()
    .AddConverter<Guid, string>(value => value.ToString("N"));

var dto = entity.Map<EntityDto>(config: config);

Mappy also supports enum-to-string, string-to-enum, enum-to-primitive, DateOnly / TimeOnly, and compatible numeric conversions while throwing MappingException on incompatible types instead of silently guessing.

Records, Constructors, and Post-Map Enrichment

Mappy matches constructor parameters by name for immutable classes and positional record types without requiring a parameterless constructor:

public sealed record ItemResponse(
    string Id,
    string Code,
    string Name,
    decimal AvailableQuantity)
{
    public ItemResponse() : this(string.Empty, string.Empty, string.Empty, 0m)
    {
    }
}

When a destination has extra fields not present on the source (such as AvailableQuantity), they default cleanly and can be populated via customMapping (for mutable/settable properties) or a with expression (for init-only record properties):

// With customMapping callback
var dto = item.Map<ItemResponse>(
    customMapping: r => r.AvailableQuantity = availableQty);

// Or with a 'with' expression on init-only record members
var immutableDto = item.Map<ItemResponse>() with { AvailableQuantity = availableQty };

Mapping into an Existing Object

Update an existing destination instance with MapTo:

request.MapTo(
    entity,
    options: new MappingOptions
    {
        IgnoreNullValues = true
    });

Setting IgnoreNullValues = true leaves existing destination properties untouched when source values are null—useful for partial/PATCH updates.

Circular References and Private Properties

Object identity tracking is enabled by default via MappingOptions.PreserveReferences:

Parent
 └── Child
      └── Parent

var result = source.Map<Destination>();
bool isSameRoot = ReferenceEquals(result, result.Child.Parent); // true

Non-public instance properties are included by default for backward compatibility and can be toggled via MappingOptions:

var options = new MappingOptions
{
    IncludeNonPublicProperties = false,
    PreserveReferences = true
};

Asynchronous Custom Mapping

Core mapping runs synchronously. Use MapAsync or MapCollectionAsync when your post-mapping enrichment requires asynchronous calls:

var dto = await source.MapAsync<UserDto>(async destination =>
{
    destination.DisplayName = await GetDisplayNameAsync(source.Id);
});

var list = await users.MapCollectionAsync<UserDto>(async destination =>
{
    destination.DisplayName = await GetDisplayNameAsync(destination.Id);
});

Performance and Design Goals

Mappy builds and caches mapping plans with compiled expression getters and setters on first use so repeated mappings avoid per-item reflection overhead:

Source + Destination + Visibility
               ↓
         MappingPlan
               ↓
   Compiled Getters / Setters
               ↓
     Cached for Reuse

Mappy intentionally avoids:

  • DI containers or global service locators
  • Heavy configuration DSLs
  • Source generators or external runtime dependencies
  • Silent, arbitrary type coercion

Target Frameworks

  • .NET Standard 2.0 (netstandard2.0)
  • .NET 10 (net10.0)

Performance Considerations

While Mappy is effective for typical scenarios, it may not be optimized for very large datasets or scenarios with high-frequency mapping needs. The performance of the library could be impacted by reflection-based operations, especially for large collections and complex nested mappings.

Contributing

Contributions to Mappy are welcome! If you find any issues or have suggestions for improvements, feel free to create a pull request or report an issue.

License

This project is open source and under the MIT License, can be freely modified and distributed.

About

Mappy is a lightweight object mapping utility for C# applications.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Contributors

Languages