Chd.Mapping.Fody
8.1.2
dotnet add package Chd.Mapping.Fody --version 8.1.2
NuGet\Install-Package Chd.Mapping.Fody -Version 8.1.2
<PackageReference Include="Chd.Mapping.Fody" Version="8.1.2" />
<PackageVersion Include="Chd.Mapping.Fody" Version="8.1.2" />
<PackageReference Include="Chd.Mapping.Fody" />
paket add Chd.Mapping.Fody --version 8.1.2
#r "nuget: Chd.Mapping.Fody, 8.1.2"
#:package Chd.Mapping.Fody@8.1.2
#addin nuget:?package=Chd.Mapping.Fody&version=8.1.2
#tool nuget:?package=Chd.Mapping.Fody&version=8.1.2
Library.Mapping.Fody – High-Performance Compile-time DTO ↔ Entity Mapper for .NET
Chd (Cleverly Handle Difficulty) library helps you cleverly handle difficulty, write code quickly, and keep your application stable.
🚩 Important Note:
When using Library.Mapping.Fody, your IDE (Visual Studio, Rider, etc.) may show red squiggly lines or error messages (such as "no implicit conversion" or "method not found") for generated mapping methods or operators.
This is normal: mapping code is injected at the IL level after compilation, so it is not visible to IntelliSense or the C# compiler during editing.
You will not get a build error if Fody is configured correctly and the build completes.
If the weaver fails or is misconfigured, runtime errors may occur.
Always ensure your project is built with Fody and check the build output for Fody logs.
📝 Table of Contents
- What is Library.Mapping.Fody?
- Features
- Installation
- Usage
- Limitations & Notes
- Build & Integration Tips
- Authors
- Acknowledgements
What is Library.Mapping.Fody?
Library.Mapping.Fody is a high-performance, compile-time object mapping library for .NET.
It uses IL weaving to generate mapping code during build, eliminating runtime reflection and manual boilerplate mapping logic.
Key Advantages
- Performance: Mapping logic is injected at compile-time. No runtime reflection.
- Type Safety: Mapping matches property names and warns for mismatches at build.
- Simplicity: Just add attributes and build. No config files, no DI, no surprises.
- Debuggability: The generated mapping code is real .NET MSIL – step through and inspect at runtime.
- No runtime dependency: Only .NET and Fody required at build.
Features
- Attribute-based mapping:
[MapTo](DTO),[MapProperty](for property name remapping) - Automatic mapping of matching properties, including collections (with identical element types)
- Property remapping via
[MapProperty("TargetPropertyName")] - Supports
implicit operatorfor DTO → Entity - Adds
MapTo()instance method for DTO → Entity mapping - No runtime mapping configuration, pure compile-time mapping
- Works in .NET Core, .NET Framework, SDK-style projects
Installation
Install NuGet package:
Install-Package Library.Mapping.FodyAdd FodyWeavers.xml in your project root:
<Weavers> <Library.Mapping/> </Weavers>Build your project:
Fody will inject mapping methods and operators into your compiled output.
Usage
Basic Mapping
using System;
using Library.Mapping;
public class UserDto
{
[MapTo(typeof(UserEntity))]
public int Id { get; set; }
public string Name { get; set; }
}
public class UserEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
class Program
{
static void Main()
{
var dto = new UserDto { Id = 1, Name = "Alice" };
// Supported mapping usages:
UserEntity entity1 = dto; // implicit operator (Fody generated)
UserEntity entity2 = dto.MapTo(); // instance MapTo method (Fody generated)
Console.WriteLine($"Id: {entity1.Id}");
Console.WriteLine($"Name: {entity1.Name}");
}
}
Console Output:
Id: 1
Name: Alice
Property Remapping
using System;
using Library.Mapping;
public class UserDto
{
[MapTo(typeof(UserEntity))]
[MapProperty("FullName")]
public string Name { get; set; }
public int Id { get; set; }
}
public class UserEntity
{
public int Id { get; set; }
public string FullName { get; set; }
}
class Program
{
static void Main()
{
var dto = new UserDto { Id = 42, Name = "Mehmet Yoldaş" };
UserEntity entity = dto.MapTo();
Console.WriteLine($"Id: {entity.Id}");
Console.WriteLine($"FullName: {entity.FullName}");
}
}
Console Output:
Id: 42
FullName: Mehmet Yoldaş
Collection Mapping
using System;
using System.Collections.Generic;
using Library.Mapping;
public class ProductDto
{
[MapTo(typeof(ProductEntity))]
public string Code { get; set; }
public int Quantity { get; set; }
}
public class ProductEntity
{
public string Code { get; set; }
public int Quantity { get; set; }
}
public class OrderDto
{
[MapTo(typeof(OrderEntity))]
public int OrderId { get; set; }
public List<ProductDto> Products { get; set; }
}
public class OrderEntity
{
public int OrderId { get; set; }
public List<ProductEntity> Products { get; set; }
}
class Program
{
static void Main()
{
var dto = new OrderDto
{
OrderId = 3,
Products = new List<ProductDto>
{
new ProductDto { Code = "A123", Quantity = 2 },
new ProductDto { Code = "B456", Quantity = 5 }
}
};
OrderEntity entity = dto.MapTo();
Console.WriteLine($"OrderId: {entity.OrderId}");
foreach (var p in entity.Products)
Console.WriteLine($"Product: {p.Code} - {p.Quantity}");
}
}
Console Output:
OrderId: 3
Product: A123 - 2
Product: B456 - 5
Limitations & Notes
- Mapping is only supported from DTO (with
[MapTo]) to the target Entity type. - Reverse mapping (Entity → DTO) is not generated.
- You cannot use
entity.MapTo()orentity.MapTo<UserDto>(). - IntelliSense and the C# compiler may not see injected members; test at runtime after building.
- Only DTO→Entity direction is supported.
- Custom mapping expressions are not supported; only property name remapping (
[MapProperty]) is available.
Build & Integration Tips
- Make sure
FodyandLibrary.Mapping.Fodypackages are installed. - Ensure
FodyWeavers.xmlexists in your project. - If mapping code doesn't appear, clean & rebuild.
- Mapping code is injected only on classes with
[MapTo(typeof(TargetType))].
Test Repository For Usage
This repository contains examples of how to use the Library.Mapping package in different scenarios, including DTO-Entity mapping, operator injection, and performance benchmarks.
Authors
Acknowledgements
- Inspired by Fody community and .NET performance advocates.
- Thanks to users reporting issues and requesting features.
For issues, feature requests, or contributions, please visit mehmet-yoldas/library-core
| Product | Versions 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 | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Chd.Mapping.Abstractions (>= 8.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.