Wiaoj.Extensions.DependencyInjection
0.0.1-alpha.32
dotnet add package Wiaoj.Extensions.DependencyInjection --version 0.0.1-alpha.32
NuGet\Install-Package Wiaoj.Extensions.DependencyInjection -Version 0.0.1-alpha.32
<PackageReference Include="Wiaoj.Extensions.DependencyInjection" Version="0.0.1-alpha.32" />
<PackageVersion Include="Wiaoj.Extensions.DependencyInjection" Version="0.0.1-alpha.32" />
<PackageReference Include="Wiaoj.Extensions.DependencyInjection" />
paket add Wiaoj.Extensions.DependencyInjection --version 0.0.1-alpha.32
#r "nuget: Wiaoj.Extensions.DependencyInjection, 0.0.1-alpha.32"
#:package Wiaoj.Extensions.DependencyInjection@0.0.1-alpha.32
#addin nuget:?package=Wiaoj.Extensions.DependencyInjection&version=0.0.1-alpha.32&prerelease
#tool nuget:?package=Wiaoj.Extensions.DependencyInjection&version=0.0.1-alpha.32&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())) - Instances (
AddSingleton(new Svc()))
- 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));
});
🔧 How It Works (Technical Details)
When you call Decorate<TService, TDecorator>(), the library performs the following steps:
- Descriptor Lookup: It finds the last registered
ServiceDescriptorforTService. - 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 | 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
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0)
- Wiaoj.Preconditions (>= 0.0.1-alpha.32)
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.Security
Authenticated encryption (AES-GCM) decorators for Wiaoj Serializers. |
|
|
Wiaoj.Serialization.Compression
Compression decorators (Gzip, Brotli) for Wiaoj Serializers. |
|
|
Tyto.Locking
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.0.1-alpha.32 | 56 | 2/22/2026 |
| 0.0.1-alpha.31 | 56 | 2/22/2026 |
| 0.0.1-alpha.30 | 50 | 2/19/2026 |
| 0.0.1-alpha.29 | 50 | 2/17/2026 |
| 0.0.1-alpha.28 | 377 | 2/11/2026 |
| 0.0.1-alpha.27 | 79 | 2/7/2026 |
| 0.0.1-alpha.26 | 56 | 2/7/2026 |
| 0.0.1-alpha.25 | 60 | 2/7/2026 |
| 0.0.1-alpha.24 | 58 | 2/7/2026 |
| 0.0.1-alpha.23 | 68 | 1/30/2026 |
| 0.0.1-alpha.22 | 233 | 1/14/2026 |
| 0.0.1-alpha.21 | 60 | 1/13/2026 |
| 0.0.1-alpha.20 | 61 | 1/12/2026 |
| 0.0.1-alpha.19 | 62 | 1/12/2026 |
| 0.0.1-alpha.18 | 61 | 1/12/2026 |
| 0.0.1-alpha.17 | 67 | 1/6/2026 |
| 0.0.1-alpha.16 | 61 | 1/4/2026 |
| 0.0.1-alpha.15 | 72 | 1/2/2026 |
| 0.0.1-alpha.14 | 158 | 12/24/2025 |
| 0.0.1-alpha.13 | 691 | 12/17/2025 |