Wiaoj.Extensions.DependencyInjection 0.0.1-alpha.32

This is a prerelease version of Wiaoj.Extensions.DependencyInjection.
dotnet add package Wiaoj.Extensions.DependencyInjection --version 0.0.1-alpha.32
                    
NuGet\Install-Package Wiaoj.Extensions.DependencyInjection -Version 0.0.1-alpha.32
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Wiaoj.Extensions.DependencyInjection" Version="0.0.1-alpha.32" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Wiaoj.Extensions.DependencyInjection" Version="0.0.1-alpha.32" />
                    
Directory.Packages.props
<PackageReference Include="Wiaoj.Extensions.DependencyInjection" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Wiaoj.Extensions.DependencyInjection --version 0.0.1-alpha.32
                    
#r "nuget: Wiaoj.Extensions.DependencyInjection, 0.0.1-alpha.32"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Wiaoj.Extensions.DependencyInjection@0.0.1-alpha.32
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Wiaoj.Extensions.DependencyInjection&version=0.0.1-alpha.32&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Wiaoj.Extensions.DependencyInjection&version=0.0.1-alpha.32&prerelease
                    
Install as a Cake Tool

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, or Transient) 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()))
  • Circular Dependency Safety: Uses ActivatorUtilities to instantiate original services, preventing StackOverflowException during 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:

  1. Descriptor Lookup: It finds the last registered ServiceDescriptor for TService.
  2. Factory Replacement: It replaces the original registration with a new implementation factory.
  3. 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.CreateInstance instead of resolving from the container recursively. This avoids the infinite loop trap common in naive decorator implementations.
  4. Decorator Composition: It instantiates the Decorator, injecting the original service instance into it.
  5. Lifetime Guard: The new registration inherits the exact same ServiceLifetime as 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
Loading failed