DhibYassine.AutoMapper 1.0.0
Mapper – Lightweight Object‑to‑Object Mapping Library
Mapper is a simple, convention‑based object mapper similar to AutoMapper.
It supports:
- ✅ Property copying by name and compatible type (including
Nullable<T>). - ✅ Recursive mapping of nested complex objects and collections (
IEnumerable<T>↔IEnumerable<T1>). - ✅ Custom mapping profiles – override mapping logic for specific type pairs.
- ✅ Cycle detection – prevents stack overflow on circular references.
- ✅ Null‑safe – returns
nullordefaultwhen source isnull. - ✅ Zero external dependencies – just .NET Standard 2.0 (+ optional DI abstractions).
- ✅ Designed for DI – can be registered with
IServiceProvider; profiles support constructor injection. - ✅ Scoped services – use
mapper.CreateScope()inside profiles to resolve scoped dependencies (e.g.,DbContext).
Installation
From NuGet.org (public)
dotnet add package DhibYassine.AutoMapper
or via Package Manager Console:
Install-Package DhibYassine.AutoMapper
From a Private BaGet Feed
If you are hosting the package on a private BaGet server, add the feed as a package source:
Visual Studio (UI):
Tools → Options → NuGet Package Manager → Package Sources → Add a new source with URLhttps://nuget.dhibyassine.com/v3/index.json.Via
NuGet.config(recommended for teams):<configuration> <packageSources> <add key="MyBaGet" value="https://nuget.dhibyassine.com/v3/index.json" /> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" /> </packageSources> </configuration>Via dotnet CLI:
dotnet nuget add source https://nuget.dhibyassine.com/v3/index.json -n MyBaGet
Note: The API key is only required when pushing packages. For installation, you only need the feed URL.
Quick Start
Basic mapping
var mapper = new Mapper();
var source = new TSource { ... };
var destination = mapper.Map<TDest>(source);
Mapping into an existing instance
mapper.Map(source, destination);
Mapping collections
var sources = new List<TSource> { ... };
var destinations = mapper.Map<List<TDest>>(sources);
Nested objects and collections
Nested complex properties and collections are mapped recursively by name/type – no extra configuration needed.
Custom Mapping Profiles
Create a profile to override convention‑based mapping:
public class MyProfile : MapperProfile<TSource, TDest>
{
public override void Map(TSource source, TDest destination, IMapper mapper)
{
// Convention already maps matching properties.
// Only override non‑trivial logic here.
destination.SomeSpecialProperty = source.FirstName + " " + source.LastName;
}
}
Profiles are automatically discovered in the entry assembly (or assemblies you provide).
No manual registration is required.
Dependency Injection (ASP.NET Core)
Register the mapper
using DhibYassine.AutoMapper;
services.AddAutoMapper(typeof(Startup).Assembly);
The IServiceProvider is used to:
- Instantiate profiles with constructor injection (e.g.,
ILogger<T>,DbContext). - Provide scoped services via
mapper.CreateScope()inside profile methods.
Using scoped services inside a profile
public class MyProfile : MapperProfile<TSource, TDest>
{
private readonly ILogger<MyProfile> _logger;
public MyProfile(ILogger<MyProfile> logger)
{
_logger = logger;
}
public override void Map(TSource source, TDest destination, IMapper mapper)
{
_logger.LogInformation("Mapping started");
using (var scope = mapper.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<MyDbContext>();
// use db to enrich destination...
}
}
}
Scanning for Profiles
By default, the mapper scans the entry assembly (Assembly.GetEntryAssembly()).
If your profiles are in a separate library, pass the assembly explicitly:
var mapper = new Mapper(serviceProvider, typeof(MyProfile).Assembly);
or register with DI:
services.AddSingleton<IMapper>(sp =>
new Mapper(sp, typeof(MyProfile).Assembly, typeof(AnotherProfile).Assembly));
API Reference
IMapper
| Method | Description |
|---|---|
TDestination Map<TDestination>(object source) |
Maps source to a new instance of TDestination. |
void Map(object source, object destination) |
Maps source into an existing destination instance. |
IServiceScope CreateScope() |
Creates a new service scope from the internal IServiceProvider (throws if none was provided). |
MapperProfile<TSource, TDest>
| Method | Description |
|---|---|
virtual void Map(TSource source, TDest destination, IMapper mapper) |
Override to apply custom logic. Convention‑based mapping runs before this method, so you only need to override non‑trivial properties. |
Advanced: Constructor Injection in Profiles
Profiles can have constructor parameters – they will be resolved from the IServiceProvider passed to the Mapper constructor.
Example:
public class MyProfile : MapperProfile<TSource, TDest>
{
private readonly ILogger<MyProfile> _logger;
private readonly MyDbContext _db;
public MyProfile(ILogger<MyProfile> logger, MyDbContext db)
{
_logger = logger;
_db = db;
}
public override void Map(TSource source, TDest destination, IMapper mapper)
{
// use _logger and _db as needed
}
}
Building from Source / Creating a NuGet Package
- Clone the repository.
- Run
dotnet pack -c Release -o ./nupkg. - The
.nupkgfile will be generated in the./nupkgfolder.
License
MIT – see LICENSE for details.
No packages depend on DhibYassine.AutoMapper.