IronMapper.Extensions.DI
1.1.4
See the version list below for details.
dotnet add package IronMapper.Extensions.DI --version 1.1.4
NuGet\Install-Package IronMapper.Extensions.DI -Version 1.1.4
<PackageReference Include="IronMapper.Extensions.DI" Version="1.1.4" />
<PackageVersion Include="IronMapper.Extensions.DI" Version="1.1.4" />
<PackageReference Include="IronMapper.Extensions.DI" />
paket add IronMapper.Extensions.DI --version 1.1.4
#r "nuget: IronMapper.Extensions.DI, 1.1.4"
#:package IronMapper.Extensions.DI@1.1.4
#addin nuget:?package=IronMapper.Extensions.DI&version=1.1.4
#tool nuget:?package=IronMapper.Extensions.DI&version=1.1.4
IronMapper
A compile-time object mapper for .NET: a Roslyn source generator writes the mapping code as plain C# when you build.
Русская версия: README.ru.md
Why IronMapper
- Mapping code you can read and debug. For every type pair the generator emits
MapTo*extension methods with explicit property assignments, so the mapping can be read and stepped through in the IDE. The generated code uses no reflection. - Configuration errors at build time, partly. For
[MapTo]/[MapFrom]an unmatched destination property is warningIM0001, a wrong[MapProperty]name is errorIM0006, an inaccessible type isIM0010(diagnostics). Profile mappings get fewer checks (IM0001is not reported for them, #22), and incompatible property types fail as a C# error in the generated file. - Flat objects are as fast as hand-written code: 11.4 ns vs 11.6 ns per object. Nested objects are slower (×1.26), see Performance.
- NativeAOT verified for the generated methods. They publish with no trim/AOT warnings and run correctly.
The optional
IMapperservice does not work under NativeAOT yet (#26). - Cold start of about 2 ms (process start plus the first mapping, over a console app without a mapper), the same order as hand-written code. AutoMapper and Mapster need tens of milliseconds there to build their mapping configuration (results).
- No runtime dependencies. The
IronMapperpackage has no NuGet dependencies and adds one assembly to the app (+28.2 KB to a framework-dependent publish). The optionalIronMapper.Extensions.DIpackage depends onMicrosoft.Extensions.DependencyInjection.Abstractions.
Quick Start
dotnet add package IronMapper
Program.cs of a console app (dotnet new console):
using IronMapper.Attributes;
using IronMapper.Generated; // required: the generated MapTo* methods live in this namespace
var entity = new UserEntity { Id = 1, Name = "Alice", Email = "alice@example.com" };
UserDto dto = entity.MapToUserDto(); // generated at build time
Console.WriteLine(dto.Name); // Alice
[MapTo(typeof(UserDto))]
public class UserEntity
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
}
public class UserDto
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
}
No IMapper and no service registration are needed.
using IronMapper.Generated; is required in every file that calls MapTo* methods. Without it the call fails
with CS1061 ("does not contain a definition for 'MapToUserDto'").
Requirements. The generator needs the C# compiler from .NET SDK 8.0.400 or later (Roslyn 4.11; Visual
Studio 2022 17.11+). On an older SDK the compiler reports warning CS9057, the generator does not run, and every
MapTo* call fails with CS1061 as well. The IronMapper package targets netstandard2.0 and net10.0; see
Platforms.
Performance
IronMapper 1.1.3 (1.1.4 generates the same code) against hand-written code (Manual), Mapperly 4.3.1, Mapster 10.0.13 and AutoMapper 16.2.0.
BenchmarkDotNet 0.15.8, .NET 10.0.12, Windows 11, Intel Core i7-12650H. Full report, method and raw data:
benchmarks/results/RESULTS.md.
| Library | Flat | Nested | Collection, 1 000 | Cold start, Δ ms | NativeAOT | Δ publish (JIT) | Δ AOT executable |
|---|---|---|---|---|---|---|---|
| Manual | 11.6 ns | 165.9 ns | 255.0 µs | 1.1 | works | +4.6 KB | +2.0 KB |
| IronMapper | 11.4 ns (×0.98) | 209.8 ns (×1.26) | 258.1 µs (×1.01) | 2.2 | works | +28.2 KB | +255.5 KB |
IronMapper IMapper |
31.0 ns (×2.66) | 227.6 ns (×1.37) | 282.4 µs (×1.11) | 23.1 | fails | +205.8 KB | +1.47 MB |
| Mapperly | 11.6 ns (×0.99) | 165.5 ns (×1.00) | 267.3 µs (×1.05) | -1.1 | works | +38.5 KB | +23.6 KB |
| Mapster | 15.6 ns (×1.34) | 192.3 ns (×1.16) | 263.5 µs (×1.03) | 99.1 | fails | +244.8 KB | +3.99 MB |
| AutoMapper | 34.6 ns (×2.97) | 270.7 ns (×1.63) | 344.6 µs (×1.35) | 73.5 | fails | +1.17 MB | +5.16 MB |
How to read it:
- Flat: one object with 15 properties. Nested: an order with a customer, an address and 5–20 lines. Collection:
List<Order>→List<OrderDto>of 1 000 nested orders. Times are means; ×N is the ratio toManual. - Nested objects are configured by hand in IronMapper (
ForMember(... MapFrom(s => s.Customer.MapToCustomerDto()))), because it does not map nested single objects automatically (see Known limitations). The other libraries map them automatically. - Cold start: median wall-clock time of process start plus one mapping, minus the same console app without a mapper (30 runs). This metric is sensitive to noise; read it as an order of magnitude.
- NativeAOT: "works" means the published executable ran and printed the correct result. The IronMapper
IMapperexecutable fails withMappingException. Mapster and AutoMapper build their mappers at run time and fail as well. - Sizes are the increase over the same console app without a mapper.
- Collection differences of a few percent (×1.01–×1.05 here; 100 000 items in the full report) are within the run-to-run spread and do not mean one library is faster.
- Measured only on .NET 10 and on one machine. Mapster.Tool (Mapster's code generator) was not measured.
Key features
All examples below assume using IronMapper.Attributes; and using IronMapper.Generated;. Full reference:
Configuration.
Attributes on the source or the destination type
[MapTo(typeof(ProductDto))] // on the source type
public class Product { /* ... */ }
[MapFrom(typeof(Order))] // on the destination type, same result
public class OrderDto { /* ... */ }
Rename, ignore, convert a property
[MapTo(typeof(InvoiceDto))]
public class Invoice
{
[MapProperty("CustomerName")] // goes to InvoiceDto.CustomerName
public string Customer { get; set; } = "";
[Ignore] // not mapped
public string InternalNote { get; set; } = "";
[MapConverter(typeof(PriceConverter))] // decimal → string through a converter
public decimal Total { get; set; }
}
public class PriceConverter : ITypeConverter<decimal, string> // IronMapper.Interfaces
{
public string Convert(decimal source) => source.ToString("F2");
}
Profiles (fluent API, similar to AutoMapper) for types you cannot annotate. The generator reads the profile at build time; it is not executed for mapping.
public class OrderProfile : MappingProfile // IronMapper.Configuration
{
public OrderProfile()
{
CreateMap<Order, OrderSummary>()
.ForMember(d => d.Address, o => o.MapFrom(s => s.Street + ", " + s.City))
.ForMember(d => d.InternalId, o => o.Ignore());
}
}
Profiles also support ReverseMap, BeforeMap/AfterMap, When, ConvertUsing, IncludeMembers and
AddTransformer; see Migrating from AutoMapper.
Collections and in-place updates
List<UserDto> list = users.MapToUserDtoList(); // from any IEnumerable<UserEntity>
UserDto[] array = users.MapToUserDtoArray();
entity.MapToUserDto(existingDto); // updates an existing object
List<T> properties (List<Comment> → List<CommentDto>) are mapped automatically when a mapping exists for the
element types.
Dependency injection (optional)
dotnet add package IronMapper.Extensions.DI
using IronMapper.Extensions.DI;
using IronMapper.Interfaces;
builder.Services.AddIronMapper(typeof(Program).Assembly); // registers IMapper and the profiles
app.MapGet("/users/{id}", (int id, IMapper mapper) => mapper.Map<UserDto>(LoadUser(id)));
IMapper finds and calls the generated methods through reflection. It is slower than the extension methods (see
the table above) and does not work under NativeAOT; prefer the extension methods when the types are known at
compile time.
Known limitations
- Nested single objects are not mapped automatically (#23). For
Customer.Address→CustomerDto.Addressthe generator emitsAddress = source.Address, and the build fails withCS0029in the generated file. Workaround:ForMember(d => d.Address, o => o.MapFrom(s => s.Address.MapToAddressDto())). Nested collections are mapped. - Positional records as destination types do not compile (
CS7036, #24): the generator always uses an object initializer. Records and classes withinitproperties work. - No flattening by convention (#25):
CustomerAddressCityis not filled fromCustomer.Address.City. UseMapFrom. IM0001is not reported for profile mappings (#22): an unmatched destination property in aCreateMapmapping keeps its default value without a warning.IMapper(IronMapper.Extensions.DI) uses reflection and does not work under NativeAOT (#26): trim warnings IL2026/IL2062/IL2072/IL2075 on publish,MappingExceptionat run time. It also has no collection overload ofMap(#8) and no per-call context (#7).- A
nullsource throwsArgumentNullException, also when it is declared nullable (UserEntity?) (#27). Mapperly, Mapster and AutoMapper returnnullthere. - Property type conversions work only where C# converts implicitly (
int→long,int→int?, …).int?→int,long→int, enum ↔ enum/string,Guid↔stringfail with a C# error in the generated file; useMapFromor a converter. Declared diagnosticsIM0002–IM0004are not reported yet. ProjectTo/IQueryableprojection is not supported (#9).
Also: diagnostics have no source location; the in-place overload ignores When, ConvertUsing and hooks;
ReverseMap() maps by name only; converters need a public parameterless constructor; private/protected nested types
cannot be mapped (IM0010); several projects in one solution have their own GeneratedMappers class. Details:
Advanced topics,
Configuration,
Diagnostics.
Platforms
- Packages:
IronMappertargetsnetstandard2.0andnet10.0;IronMapper.Extensions.DItargetsnetstandard2.1andnet10.0. The generator needs .NET SDK 8.0.400+. - The library tests run on .NET 10 (Ubuntu and Windows in CI). The equivalence tests of the benchmark suite also run on .NET 8. .NET 9 is not tested in CI (#10). The Quick Start above was checked by hand on .NET 8, 9 and 10.
Roadmap
Open issues:
ProjectTo<TDestination>()forIQueryable— #9Map<List<T>>(source)throughIMapper— #8- Context data in
Mapcalls (likeResolutionContext.Items) — #7 net9.0target — #10
Next release, 1.2.0: faster generated code for nested objects and collections, IMapper without reflection
(NativeAOT, #26), IM0001 for profiles behind an opt-in switch (#22), diagnostics with source locations. The
working plan (in Russian) is IRONMAPPER_PLAN.md;
it may change.
Documentation
- Getting started
- Configuration reference
- Advanced topics
- Migrating from AutoMapper
- Compiler diagnostics
- Benchmarks
- Changelog
Demo. samples/IronMapper.Demo is
a console app with eight scenarios, most of them on data from the Open Library API (no API key needed): attributes, profiles, collections, nested objects mapped separately,
a custom converter, conditional mapping (When), hooks with ReverseMap, and IncludeMembers.
git clone https://github.com/iron-mapper/IronMapper
cd IronMapper
dotnet run --project samples/IronMapper.Demo
Contributing
- Fork the repository and create a branch from
main. - Make the change with tests.
dotnet build IronMapper.slnxanddotnet test IronMapper.slnxmust pass without warnings.- Open a pull request against
main; add a line toCHANGELOG.mdunder[Unreleased].
Code style: .editorconfig.
License
MIT © 2026 Aleksey Mironov
| 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 is compatible. 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 | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | 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.1
- IronMapper (>= 1.1.4)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0)
-
net10.0
- IronMapper (>= 1.1.4)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.