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.
- 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
recordmapping, 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
MapTofor updating existing instances andcustomMapping/withfor post-map enrichment- Detailed
MappingExceptiondiagnostics (SourceType,DestinationType,MemberName) - Targets
netstandard2.0andnet10.0with no external runtime dependencies
dotnet add package Mappy.dotNet --version 4.2.0
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>();Mappy recursively maps compatible nested objects:
Order
└── Customer
↓
OrderDto
└── CustomerDto
var dto = order.Map<OrderDto>();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).
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");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.
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 };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.
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); // trueNon-public instance properties are included by default for backward compatibility and can be toggled via MappingOptions:
var options = new MappingOptions
{
IncludeNonPublicProperties = false,
PreserveReferences = true
};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);
});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
.NET Standard 2.0(netstandard2.0).NET 10(net10.0)
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.
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.
This project is open source and under the MIT License, can be freely modified and distributed.
