Toastie.DependencyInjection (2.3.2)

Published 2024-07-10 01:10:12 -07: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 2.3.2 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 7.0.0 net7.0
Details
NuGet
2024-07-10 01:10:12 -07:00
10
Toastie
302 KiB
Assets (2)
Versions (5) View all
2.3.2 2024-07-10
2.3.1 2024-07-10
2.3.0 2024-07-10
2.2.1 2024-07-10
2.2.0 2024-07-10