DependencyInjection.AdvancedExtensions 1.0.0

dotnet add package DependencyInjection.AdvancedExtensions --version 1.0.0
                    
NuGet\Install-Package DependencyInjection.AdvancedExtensions -Version 1.0.0
                    
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="DependencyInjection.AdvancedExtensions" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DependencyInjection.AdvancedExtensions" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DependencyInjection.AdvancedExtensions" />
                    
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 DependencyInjection.AdvancedExtensions --version 1.0.0
                    
#r "nuget: DependencyInjection.AdvancedExtensions, 1.0.0"
                    
#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 DependencyInjection.AdvancedExtensions@1.0.0
                    
#: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=DependencyInjection.AdvancedExtensions&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DependencyInjection.AdvancedExtensions&version=1.0.0
                    
Install as a Cake Tool

Dependency Injection (DI) Extensions for .NET

A set of advanced Dependency Injection (DI) extensions for .NET applications. This library includes enhancements to the standard DI framework, providing additional functionality for custom lifetimes, scoped services, session-based services, interceptors, and service decorators. It aims to simplify and extend the flexibility of service registration and management within the DI container.

Features

  • Custom Service Lifetimes: Add services with custom lifetimes (per user session, per request, etc.).
  • Method Interceptors: Intercept method calls to services for custom behavior.
  • Asynchronous Interceptors: Intercept asynchronous method calls.
  • Lazy-Loaded Services: Delay service initialization until it's actually needed.
  • Session-Based Services: Handle services that should persist throughout a user's session.
  • Decorator Pattern: Add decorators to services for additional functionality.

Installation

You can install this package via NuGet:

dotnet add package DependencyInjection.Extensions

Or, via the NuGet package manager in Visual Studio:

  1. Right-click on your project in Solution Explorer.
  2. Click on "Manage NuGet Packages".
  3. Search for DependencyInjection.Extensions and click "Install".

Usage

1. Custom Service Lifetimes

You can register services with custom lifetimes using the extensions provided by this library.

public void ConfigureServices(IServiceCollection services)
{
    // Register a service with a custom lifetime (per user session)
    services.AddSingletonPerUserSession<MyService>();
}

2. Method Interceptors

Add method-level interceptors to modify service behavior.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMethodInterceptor<IMyService>(service =>
    {
        // Custom logic to execute before or after method calls
    });
}

3. Asynchronous Interceptors

Add asynchronous interceptors for asynchronous service methods.

public void ConfigureServices(IServiceCollection services)
{
    services.AddAsyncInterceptor<IMyService>(async service =>
    {
        // Custom async logic to execute before or after asynchronous calls
    });
}

4. Lazy Interceptors

Use lazy initialization for deferred service creation.

public void ConfigureServices(IServiceCollection services)
{
    services.AddLazyInterceptor<IMyService>(service =>
    {
        // Lazy initialization logic
        return new Lazy<IMyService>(() => service);
    });
}

5. Session-Based Services

Register services that are tied to the user session, ensuring they are cleaned up when the session ends.

public void ConfigureServices(IServiceCollection services)
{
    services.AddPerUser<MyService>();
}

6. Service Decorators

Add decorators to wrap existing services with additional behavior.

public void ConfigureServices(IServiceCollection services)
{
    services.AddDecorator<IMyService>(
        typeof(MyServiceDecorator1),
        typeof(MyServiceDecorator2)
    );
}

Contributing

We welcome contributions to this project. If you'd like to contribute, please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Make your changes and commit them.
  4. Open a pull request to the main branch of this repository.

Please ensure that your code adheres to the existing style and includes tests for any new functionality or changes.

License

This project is licensed under the MIT License - see the LICENSE file for details.


Example Code

Here's an example of how to register and use session-based services:

public class MyService : IDisposable
{
    public void Dispose()
    {
        // Cleanup logic
    }
}

// In your ConfigureServices method
public void ConfigureServices(IServiceCollection services)
{
    services.AddPerUser<MyService>();
}

This will register MyService to be tied to the user session, and will ensure that the service is disposed of when the session ends.

Product 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 was computed.  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 was computed.  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

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.0 116 1/5/2025