Wiaoj.Extensions.DependencyInjection
0.5.1-alpha
dotnet add package Wiaoj.Extensions.DependencyInjection --version 0.5.1-alpha
NuGet\Install-Package Wiaoj.Extensions.DependencyInjection -Version 0.5.1-alpha
<PackageReference Include="Wiaoj.Extensions.DependencyInjection" Version="0.5.1-alpha" />
<PackageVersion Include="Wiaoj.Extensions.DependencyInjection" Version="0.5.1-alpha" />
<PackageReference Include="Wiaoj.Extensions.DependencyInjection" />
paket add Wiaoj.Extensions.DependencyInjection --version 0.5.1-alpha
#r "nuget: Wiaoj.Extensions.DependencyInjection, 0.5.1-alpha"
#:package Wiaoj.Extensions.DependencyInjection@0.5.1-alpha
#addin nuget:?package=Wiaoj.Extensions.DependencyInjection&version=0.5.1-alpha&prerelease
#tool nuget:?package=Wiaoj.Extensions.DependencyInjection&version=0.5.1-alpha&prerelease
Wiaoj Dependency Injection Extensions
A lightweight, high-performance extension library for Microsoft.Extensions.DependencyInjection. This package provides a robust implementation of the Decorator Pattern, allowing you to wrap registered services with cross-cutting concerns (like logging, caching, or resilience policies) without altering their underlying registration logic.
Built using modern C# features, it ensures type safety, preserves service lifetimes, and handles complex dependency chains effortlessly.
🌟 Features
- Decorator Pattern Made Easy: Apply decorators to services with a single line of code.
- Lifetime Preservation: Automatically respects the lifetime (
Singleton,Scoped, orTransient) of the original service. - Universal Compatibility: Works with all service registration types:
- Implementation Type (
AddSingleton<ISvc, Svc>) - Factory Delegates (
AddScoped(sp => new Svc())) - Existing instances, including
AddKeyedSingleton<ISvc>(key, instance). The decorator receives the exact supplied instance; for a distinct wrapper the container owns that wrapper, not its caller-owned inner instance. - Open generic decoration is not supported; register and decorate closed generic services.
- Implementation Type (
- Circular Dependency Safety: Uses
ActivatorUtilitiesto instantiate original services, preventingStackOverflowExceptionduring resolution. - Modern Syntax: Leverages C# "Explicit Extension" syntax for cleaner API surface.
📦 Installation
dotnet add package Wiaoj.Extensions.DependencyInjection
🚀 Usage
1. Type-Based Decoration
The most common scenario is wrapping an interface with a decorator class that accepts the inner service in its constructor.
Scenario: You have an ICalculator and want to add logging without changing the logic.
// 1. Define your service
public interface ICalculator { int Add(int a, int b); }
public class Calculator : ICalculator { ... }
// 2. Define your decorator
public class LoggingCalculatorDecorator : ICalculator {
private readonly ICalculator _inner;
private readonly ILogger<LoggingCalculatorDecorator> _logger;
// The inner service is injected automatically
public LoggingCalculatorDecorator(ICalculator inner, ILogger<LoggingCalculatorDecorator> logger) {
_inner = inner;
_logger = logger;
}
public int Add(int a, int b) {
_logger.LogInformation($"Adding {a} and {b}");
return _inner.Add(a, b);
}
}
// 3. Register and Decorate
var services = new ServiceCollection();
// Register the base service
services.AddSingleton<ICalculator, Calculator>();
// Apply the decorator using Wiaoj extensions
services.Decorate<ICalculator, LoggingCalculatorDecorator>();
2. Factory-Based Decoration
For more complex scenarios where manual construction is required (e.g., passing specific parameters or resolving dependencies manually).
services.AddScoped<IUserService, UserService>();
services.Decorate<IUserService>((innerService, provider) => {
var cache = provider.GetRequiredService<IMemoryCache>();
// Manually create the decorator
return new CachingUserServiceDecorator(innerService, cache, expiration: TimeSpan.FromMinutes(10));
});
Assembly scanning and Native AOT
services.Scan(...) discovers exported types and their interfaces at runtime. Use
it on ordinary, untrimmed JIT hosts. Assembly scanning is not supported in
trimmed or Native AOT apps: types and constructors can be removed. The package's
IsAotCompatible metadata is not a guarantee about the scanning API.
Keep the same implementation types and lifetimes, but register them explicitly in the AOT application's composition root. For example, replace a scan that registers these interfaces as singleton, scoped and transient respectively with:
services.AddSingleton<ISingletonProbe, SingletonProbe>();
services.AddScoped<IScopedProbe, ScopedProbe>();
services.AddTransient<ITransientProbe, TransientProbe>();
With explicit registrations the container can resolve the known service types without enumerating assemblies. Check each separate feature you use for trimming/AOT compatibility; this does not certify all decorator or reflection APIs.
The package smoke packs
the candidate and compares the same service graph and lifetimes in two clean
consumers: one executes ordinary JIT scanning, and the other runs the published
Linux x64 Native AOT executable using only explicit registration. An additional
compile-only consumer verifies that calling the packaged Scan API with trim
analysis enabled produces IL2026 at the call site. The unsupported scanner is
not executed in that check. Run
bash .github/package-smoke/di-registration/run.sh on Linux x64 with the pinned
SDK and the Native AOT C toolchain. The optional argument is an absolute offline
NuGet feed directory with the required SDK/runtime dependencies.
🔧 How It Works (Technical Details)
When you call Decorate<TService, TDecorator>(), the library performs the following steps:
- Descriptor Lookup: It finds matching
ServiceDescriptorentries forTService. - Factory Replacement: It replaces the original registration with a new implementation factory.
- Instance Resolution: Inside the new factory:
- It resolves or creates the Original Service instance. Critically, if the original service was registered via type (not factory), it creates the instance using
ActivatorUtilities.CreateInstanceinstead of resolving from the container recursively. This avoids the infinite loop trap common in naive decorator implementations.
- It resolves or creates the Original Service instance. Critically, if the original service was registered via type (not factory), it creates the instance using
- Decorator Composition: It instantiates the Decorator, injecting the original service instance into it.
- Lifetime Guard: The new registration inherits the exact same
ServiceLifetimeas the original one.
🤝 Contributing
Contributions are welcome! Please ensure that any PRs maintain the existing code style and include unit tests covering the new functionality.
📄 License
Licensed under the MIT License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Wiaoj.Preconditions (>= 0.5.1-alpha)
NuGet packages (5)
Showing the top 5 NuGet packages that depend on Wiaoj.Extensions.DependencyInjection:
| Package | Downloads |
|---|---|
|
Wiaoj.Serialization.DependencyInjection
ASP.NET Core dependency injection integration for Wiaoj Serialization. |
|
|
Wiaoj.Ddd
Core implementation for Wiaoj DDD, including in-memory Domain Event Dispatcher and DI extensions. |
|
|
Wiaoj.Serialization.Compression
Compression decorators (Gzip, Brotli) for Wiaoj Serializers. |
|
|
Wiaoj.Serialization.Security
Encrypts what a Wiaoj serializer writes with a Wiaoj.Security key ring: authenticated AES-GCM that follows key rotation. |
|
|
Tyto.Locking
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.5.1-alpha | 0 | 12/6/2026 |
| 0.5.0-alpha.1 | 0 | 12/5/2026 |
| 0.4.2-alpha.2 | 69 | 12/3/2026 |
| 0.4.2-alpha.1 | 95 | 12/2/2026 |
| 0.4.1-alpha.3 | 111 | 11/30/2026 |
| 0.4.1-alpha.2 | 112 | 11/29/2026 |
| 0.4.1-alpha.1 | 119 | 11/28/2026 |
| 0.4.0-alpha.5 | 139 | 11/27/2026 |
| 0.4.0-alpha.4 | 123 | 11/15/2026 |
| 0.4.0-alpha.3 | 118 | 11/4/2026 |
| 0.4.0-alpha.1 | 117 | 10/30/2026 |
| 0.3.0-alpha.1 | 2,611 | 10/5/2026 |
| 0.2.0-alpha.3 | 116 | 9/24/2026 |
| 0.2.0-alpha.2 | 118 | 9/24/2026 |
| 0.2.0-alpha.1 | 119 | 9/24/2026 |
| 0.1.0-alpha.9 | 305 | 9/21/2026 |
| 0.1.0-alpha.8 | 114 | 9/21/2026 |
| 0.1.0-alpha.7 | 2,155 | 9/18/2026 |
| 0.1.0-alpha.6 | 122 | 9/16/2026 |
| 0.1.0-alpha.5 | 119 | 9/16/2026 |