Skip to content

voidnone/dependency-injection

Repository files navigation

VoidNone.DependencyInjection

A lightweight .NET dependency injection extension library that supports automatic service registration via attributes.

Features

  • Attribute-based registration: Mark classes with [Singleton], [Scoped], or [Transient] attributes
  • Interface support: Register services with one or multiple interface types
  • Keyed services: Support for keyed service registration (requires .NET 8+)
  • Generic attributes: Convenient generic attribute syntax (requires .NET 8+)

Installation

Nuget

dotnet add package VoidNone.DependencyInjection

Quick Start

1. Mark your services

[Singleton]
class DatabaseService { }

[Scoped]
class UserService { }

[Transient]
class LoggerService { }

2. Register services

services.AddFromAssemblies(typeof(Service1).Assembly);

Usage

Service Lifetime Attributes

Attribute Description
[Singleton] Single instance for the lifetime of the application
[Scoped] One instance per request/scope
[Transient] New instance each time it's requested

Registering with Interfaces

interface IService { }
interface IBaseService { }

[Singleton<IService>]
class MyService : IService { }

// Register with multiple interfaces
[Singleton<IService, IBaseService>]
class MyService2 : IService, IBaseService { }

Keyed Services (.NET 8+)

[Singleton<IService>("primary")]
class PrimaryService : IService { }

[Singleton<IService>("backup")]
class BackupService : IService { }

Extended Service Retrieval

var provider = services
    .AddFromAssemblies(typeof(MyService).Assembly)
    .BuildServiceProvider();

// AddFromAssemblies registers the IServiceCollection itself so metadata helpers can inspect it later

// Get all implementation types for a service
Type[] types = provider.GetAllServiceTypes<IService>();

// Get all service instances (including keyed services) (.NET 8+)
IEnumerable<IService> instances = provider.GetAllServices<IService>();

License

MIT

About

Lightweight .NET dependency injection extension library that supports automatic service registration via attributes.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages