Library.Mapping.Fody
8.2.6
Use this package Chd.Mapping.Fody
dotnet add package Library.Mapping.Fody --version 8.2.6
NuGet\Install-Package Library.Mapping.Fody -Version 8.2.6
<PackageReference Include="Library.Mapping.Fody" Version="8.2.6" />
<PackageVersion Include="Library.Mapping.Fody" Version="8.2.6" />
<PackageReference Include="Library.Mapping.Fody" />
paket add Library.Mapping.Fody --version 8.2.6
#r "nuget: Library.Mapping.Fody, 8.2.6"
#:package Library.Mapping.Fody@8.2.6
#addin nuget:?package=Library.Mapping.Fody&version=8.2.6
#tool nuget:?package=Library.Mapping.Fody&version=8.2.6
Library.Mapping
What is Library.Mapping?
Library.Mapping is a high-performance, compile-time object mapper for .NET applications. It generates mapping code during your build, eliminating both the runtime reflection overhead and common type-safety pitfalls of traditional mappers.
Why Compile-time Mapping?
- Performance: Mapping logic is generated and inlined during compilation. No reflection or emitted code at runtime.
- Type Safety: Catches potential mapping errors during build instead of at runtime.
- IDE Support: Full access to code analysis, refactoring tools, and intellisense.
- Debuggability: Trace actual mapping logic in code, set breakpoints, and step through mappings.
Compile-time vs Runtime Mapping
| Feature | Compile-time Mapping (Library.Mapping) | Runtime Mapping (AutoMapper/Mapster) |
|---|---|---|
| Performance | Fastest (Ahead-of-Time, no reflection) | Slower (Reflection/Emission) |
| Type Safety | Compile-time error detection | Runtime errors possible |
| Refactoring | IDE-friendly, easy renaming | Mapping config requires manual update |
| Debugging | Set breakpoints, step over code | Hard to debug, dynamic code |
| Deployment | No mapper runtime dependency | Needs mapping lib at runtime |
.NET Benchmark Results
| Scenario | AutoMapper (ms) | Mapster (ms) | Library.Mapping (ms) |
|---|---|---|---|
| 1M DTO→Entity Map (Release x64) | 980 | 410 | 180 |
| 100K Entity→DTO Map with Nesting | 180 | 74 | 34 |
| Flat object, assign all properties | 22 | 10 | 4 |
Benchmark code in /benchmarks/Mapping.Benchmark.
Installation
1. Install NuGet package:
Install-Package Library.Mapping.Fody
2. Add FodyWeavers.xml: In the root of your project, add or update:
<Weavers>
<Library.Mapping/>
</Weavers>
3. Build project:
- The weaver will generate mapper implementations during build.
Code Samples: Modern DTO-Entity Mapping
Define DTO and Entity:
using Library.Mapping;
public class UserDto {
[MapTo(typeof(UserEntity))]
public int Id { get; set; }
public string Name { get; set; }
}
public class UserEntity {
[MapFrom(typeof(UserDto))]
public int Id { get; set; }
public string Name { get; set; }
}
Operator Injection
Both source and target can define implicit/explicit conversion operators, or use provided extension methods:
var entity = userDto.MapTo<UserEntity>(); // Extension method
UserEntity entity2 = userDto; // Operator overload (if injected by mapping)
Usage
After building, mapping methods and conversions are generated. Example usage:
var dto = new UserDto { Id = 1, Name = "Alice" };
UserEntity entity = dto; // Implicitly mapped
var dto2 = entity.MapTo<UserDto>(); // Back to DTO
- All mappings are validated at compile-time.
- Customization via mapping attributes available (see wiki).
Build & Integration Tips
- Confirm
Fodypackages are installed and correctFodyWeavers.xmlexists. - Clean & rebuild if generated code isn't appearing.
- Compatible with .NET Core, .NET Framework, and SDK-style projects.
- No runtime dependency except standard BCL.
Test & Benchmark
- See
/benchmarks/Mapping.Benchmarkproject for reproducible performance data. - CI runs mapping integration tests on PRs.
Authors
- Mehmet Yoldaş (mehmet-yoldas)
Acknowledgements
- Inspired by Fody community and .NET performance advocates.
- Thanks to users reporting issues and requesting features.
| 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.1)
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 |
|---|