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.
  • 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 the IServiceCollection.RegisterServices extension method.

Using the registration attributes

  1. Registering the type directly
// Registration.
[Service(ServiceLifetime.Singleton)]
public sealed class MyService { ... }

// Resolution.
serviceProvider.GetRequiredService<MyService>();
  1. Registering the type through another type.
// Registration.
[Service<ISomething>(ServiceLifetime.Scoped)]
public sealed class MyService : ISomething { ... }

// Resolution.
serviceProvider.GetRequiredService<ISomething>();
  1. 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 { ... }
  1. 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
NuGet
2025-01-03 13:20:00 +00:00
3
Toastie
358 KiB
Assets (5)
Versions (6) View all
3.0.0 2025-01-03
2.3.2 2024-11-14
2.3.1 2024-11-14
2.3.0 2024-11-14
2.2.1 2024-11-14