Pitasoft.ObjectMapping.Generator.DependencyInjectionExtensions
1.0.1
dotnet add package Pitasoft.ObjectMapping.Generator.DependencyInjectionExtensions --version 1.0.1
NuGet\Install-Package Pitasoft.ObjectMapping.Generator.DependencyInjectionExtensions -Version 1.0.1
<PackageReference Include="Pitasoft.ObjectMapping.Generator.DependencyInjectionExtensions" Version="1.0.1" />
<PackageVersion Include="Pitasoft.ObjectMapping.Generator.DependencyInjectionExtensions" Version="1.0.1" />
<PackageReference Include="Pitasoft.ObjectMapping.Generator.DependencyInjectionExtensions" />
paket add Pitasoft.ObjectMapping.Generator.DependencyInjectionExtensions --version 1.0.1
#r "nuget: Pitasoft.ObjectMapping.Generator.DependencyInjectionExtensions, 1.0.1"
#:package Pitasoft.ObjectMapping.Generator.DependencyInjectionExtensions@1.0.1
#addin nuget:?package=Pitasoft.ObjectMapping.Generator.DependencyInjectionExtensions&version=1.0.1
#tool nuget:?package=Pitasoft.ObjectMapping.Generator.DependencyInjectionExtensions&version=1.0.1
Pitasoft.ObjectMapping.Generator.DependencyInjectionExtensions
<a name="english"></a>
English
Description
This library provides dependency injection (DI) extension methods for the Pitasoft.ObjectMapping.Generator. It simplifies the registration of source-generated mappers into the standard Microsoft.Extensions.DependencyInjection container.
It automatically scans your assemblies for classes decorated with the [ObjectMapper] attribute and registers them, allowing you to inject their interfaces or the classes themselves directly into your services.
Installation
Install via NuGet:
dotnet add package Pitasoft.ObjectMapping.Generator.DependencyInjectionExtensions
Key Features
- Automatic Registration: Scans assemblies for
[ObjectMapper]attributes. - Interface Support: Automatically registers all interfaces implemented by the mapper classes.
- Flexible Lifetime: Supports Singleton (default), Scoped, and Transient lifetimes.
- Fluent API: Easy to use within
Program.csorStartup.cs. - Multi-Framework Support: Targets .NET 8, .NET 9, and .NET 10.
Practical Examples
1. Define your Mapper
Using Pitasoft.ObjectMapping.Generator, define your mapper and decorate it with [ObjectMapper].
using Pitasoft.ObjectMapping;
namespace MyApp.Mappers;
public interface IUserMapper
{
UserDto MapToDto(User user);
}
[ObjectMapper]
public partial class UserMapper : IUserMapper
{
// Implementation is generated by the Source Generator
}
2. Register Mappers in DI
In your Program.cs, use AddObjectMappers() to register all mappers in the calling assembly.
using Microsoft.Extensions.DependencyInjection;
using Pitasoft.ObjectMapping.Generator.DependencyInjectionExtensions;
var builder = WebApplication.CreateBuilder(args);
// Register all mappers found in the current assembly as Singletons
builder.Services.AddObjectMappers();
// OR: Register from specific assemblies
builder.Services.AddObjectMappers(typeof(UserMapper).Assembly);
// OR: Specify a different lifetime
builder.Services.AddObjectMappers(ServiceLifetime.Scoped);
var app = builder.Build();
3. Inject and Use
Inject the interface (or the class) into your services or controllers.
public class UserService(IUserMapper userMapper)
{
public UserDto GetUserById(int id)
{
var user = _repository.Find(id);
return userMapper.MapToDto(user);
}
}
<a name="español"></a>
Español
Descripción
Esta librería proporciona métodos de extensión para la Inyección de Dependencias (DI) de Pitasoft.ObjectMapping.Generator. Facilita el registro de los mapeadores generados por código (Source Generated) en el contenedor estándar de Microsoft.Extensions.DependencyInjection.
Escanea automáticamente tus ensamblados en busca de clases decoradas con el atributo [ObjectMapper] y las registra, permitiéndote inyectar sus interfaces o las clases mismas directamente en tus servicios.
Instalación
Instalar a través de NuGet:
dotnet add package Pitasoft.ObjectMapping.Generator.DependencyInjectionExtensions
Características Principales
- Registro Automático: Escanea ensamblados buscando el atributo
[ObjectMapper]. - Soporte de Interfaces: Registra automáticamente todas las interfaces implementadas por las clases mapeadoras.
- Ciclo de Vida Flexible: Soporta Singleton (predeterminado), Scoped y Transient.
- API Fluida: Fácil de usar en
Program.csoStartup.cs. - Soporte Multi-Framework: Compatible con .NET 8, .NET 9 y .NET 10.
Ejemplos Prácticos
1. Define tu Mapeador
Usando Pitasoft.ObjectMapping.Generator, define tu mapeador y decóralo con [ObjectMapper].
using Pitasoft.ObjectMapping;
namespace MyApp.Mappers;
public interface IUserMapper
{
UserDto MapToDto(User user);
}
[ObjectMapper]
public partial class UserMapper : IUserMapper
{
// La implementación es generada por el Source Generator
}
2. Registrar Mapeadores en DI
En tu Program.cs, usa AddObjectMappers() para registrar todos los mapeadores del ensamblado actual.
using Microsoft.Extensions.DependencyInjection;
using Pitasoft.ObjectMapping.Generator.DependencyInjectionExtensions;
var builder = WebApplication.CreateBuilder(args);
// Registra todos los mapeadores encontrados en el ensamblado actual como Singletons
builder.Services.AddObjectMappers();
// O: Registrar desde ensamblados específicos
builder.Services.AddObjectMappers(typeof(UserMapper).Assembly);
// O: Especificar un ciclo de vida diferente
builder.Services.AddObjectMappers(ServiceLifetime.Scoped);
var app = builder.Build();
3. Inyectar y Usar
Simplemente inyecta la interfaz (o la clase) en tus servicios o controladores.
public class UserService(IUserMapper userMapper)
{
public UserDto GetUserById(int id)
{
var user = _repository.Find(id);
return userMapper.MapToDto(user);
}
}
API Reference / Referencia de la API
| Method / Método | Description / Descripción |
|---|---|
AddObjectMappers() |
Registers all mappers in the calling assembly as Singletons. / Registra todos los mapeadores en el ensamblado que llama como Singletons. |
AddObjectMappers(params Assembly[] assemblies) |
Registers mappers from specific assemblies. / Registra mapeadores de ensamblados específicos. |
AddObjectMappers(ServiceLifetime lifetime, params Assembly[] assemblies) |
Registers mappers with a specific lifetime. / Registra mapeadores con un ciclo de vida específico. |
AddObjectMapper<TMapper>() |
Registers a specific mapper type. / Registra un tipo de mapeador específico. |
Autor
Sebastián Martínez Pérez
Licencia
Copyright © 2026 Pitasoft, S.L.
Licenciado bajo los términos de la LICENSE.txt incluida en este repositorio.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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 is compatible. 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. |
-
net10.0
-
net8.0
-
net9.0
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 |
|---|---|---|
| 1.0.1 | 164 | 4/19/2026 |