Toastie.DependencyInjection (3.0.0)
Published 2025-01-03 13:20:00 +00:00 by toastie_t0ast
Installation
dotnet nuget add source --name toastie_t0ast --username your_username --password your_token
dotnet add package --source toastie_t0ast --version 3.0.0 Toastie.DependencyInjection
About this package
Provides utility methods and attributes for "Microsoft.Extensions.DependencyInjection".
Toastie.DependencyInjection
- Dependencies:
Microsoft.Extensions.DependencyInjection.Abstractions
Defines the following extension methods:
- IServiceCollection Extensions
- RegisterServices: Registers all classes and structs from an assembly that have a
ServiceAttributeBase
attribute applied to them.
- RegisterServices: Registers all classes and structs from an assembly that have a
- IServiceProvider and IServiceScopeFactory Extensions
- GetParameterizedService: Gets a service that requires arguments that are not registered in the IoC container.
- GetScopedService: Gets a service that needs to be resolved from a scope.
Defines the following types:
- ServiceAttributeBase: It's the base class for dependency injection registrations.
- ServiceAttribute: Marks a class or struct for registration via the
IServiceCollection.RegisterServices
extension method. - ServiceAttribute<T>: Marks a class or struct for registration under the type
T
via theIServiceCollection.RegisterServices
extension method.
Using the registration attributes
- Registering the type directly
// Registration.
[Service(ServiceLifetime.Singleton)]
public sealed class MyService { ... }
// Resolution.
serviceProvider.GetRequiredService<MyService>();
- Registering the type through another type.
// Registration.
[Service<ISomething>(ServiceLifetime.Scoped)]
public sealed class MyService : ISomething { ... }
// Resolution.
serviceProvider.GetRequiredService<ISomething>();
- Registering multiple types under the same type.
[Service<ISomething>(ServiceLifetime.Transient, true)]
public sealed class MyService1 : ISomething { ... }
[Service<ISomething>(ServiceLifetime.Transient, true)]
public sealed class MyService2 : ISomething { ... }
[Service<ISomething>(ServiceLifetime.Transient, true)]
public sealed class MyService3 : ISomething { ... }
- Registering the services in the
IServiceCollection
.
// To register services from the current assembly.
serviceCollection.RegisterServices();
// OR
// To register services from a specific assembly.
serviceCollection.RegisterServices(someAssembly);
Dependencies
ID | Version | Target Framework |
---|---|---|
Microsoft.Extensions.DependencyInjection.Abstractions | 8.0.2 | net8.0 |
Microsoft.Extensions.DependencyInjection.Abstractions | 9.0.0 | net9.0 |
Details
Assets (5)
Versions (6)
View all