Headroomov.DependencyInjection.Extensions 10.0.1

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

Headroomov.DependencyInjection.Extensions

This package provides strict dependency injection (DI) extensions that enforce consistent service lifetime registration between interfaces and their implementations using ServiceLifetimeAttribute. It ensures that mismatches in lifetimes between interfaces and their implementations are caught at compile-time, making your DI setup reliable and predictable.

Features

  • Lifetime Consistency Enforcement: Guarantees that interfaces and their implementations have matching lifetimes.
  • Automatic Service Registration: Simplifies adding services to DI containers while adhering to strict lifetime contracts.
  • Error Prevention: Throws clear exceptions if the lifetimes of interfaces and implementations don’t match or if an attribute is missing.
  • Flexible API: Supports both generic and non-generic registration methods.

Installation

To install via NuGet, use the following command:

Install-Package Headroomov.DependencyInjection.Extensions

Or with the .NET CLI:

dotnet add package Headroomov.DependencyInjection.Extensions

Usage

Registering Services with Lifetime Attributes

The core of this package is the AddWithAttribute method, which ensures that both interfaces and their implementations are registered in the DI container with consistent lifetimes as defined by ServiceLifetimeAttribute.

Example: Interface and Implementation Registration Using Generics
using Microsoft.Extensions.DependencyInjection;
using Headroomov.DependencyInjection.Extensions;
using Headroomov.DependencyInjection.Extensions.Attributes;

[ServiceLifetime(ServiceLifetime.Singleton)]
public interface IMyService
{
    void Execute();
}

[ServiceLifetime(ServiceLifetime.Singleton)]
public class MyService : IMyService
{
    public void Execute()
    {
        // Implementation here
    }
}

public class DependencyInjection
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Register both the interface and the implementation with matching lifetimes using generics
        services.AddWithAttribute<IMyService, MyService>();
    }
}

Explanation:

  • AddWithAttribute<TInterface, TImplementation> ensures that both the interface and the implementation are checked for ServiceLifetimeAttribute, and their lifetimes must match. If they don’t, an exception will be thrown.
  • This method is preferred as it leverages C#'s type safety with generics.
Example: Direct Service Registration Using Types
[ServiceLifetime(ServiceLifetime.Singleton)]
public class MySingletonService
{
    // Implementation here
}

public class DependencyInjection
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Register service directly with its lifetime defined in the attribute
        services.AddWithAttribute(typeof(MySingletonService), typeof(MySingletonService));
    }
}

Explanation of Non-Generic Method (Backup):

You can also use the non-generic method to register services. This approach is not recommended but is provided for flexibility:

public class DependencyInjection
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Backup method: Register interface and implementation using types
        services.AddWithAttribute(typeof(IMyService), typeof(MyService));
    }
}

This method uses Type instead of generics, which makes it a bit less type-safe, but still functional.

Example: Scoped Service Registration

[ServiceLifetime(ServiceLifetime.Scoped)]
public class MyScopedService
{
    // Implementation for Scoped service
}

public class DependencyInjection
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Register Scoped service using generics
        services.AddWithAttribute<MyScopedService, MyScopedService>();
    }
}

Key Points

  • Interfaces and implementations must both have the ServiceLifetimeAttribute applied. This ensures that the AddWithAttribute method checks for lifetime consistency.
  • The method will throw an exception if the lifetime contracts don’t match between an interface and its implementation.
  • Preferred method: Use the generic version AddWithAttribute<TInterface, TImplementation> for better type safety and simplicity.
  • Backup method: You can use the AddWithAttribute with Type parameters (AddWithAttribute(typeof(IMyService), typeof(MyService))), but it is not as type-safe as the generic version.

License

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

Contact

For any questions or issues, feel free to open an issue or contact us directly at Headroomov.

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

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
10.0.1 138 2/3/2026