Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lordmike/mbw.utilities.di.named

Named DI for Microsoft.Extensions.DependencyInjection
https://github.com/lordmike/mbw.utilities.di.named

Last synced: about 11 hours ago
JSON representation

Named DI for Microsoft.Extensions.DependencyInjection

Awesome Lists containing this project

README

        

## MBW.Utilities.DI.Named [![Generic Build](https://github.com/LordMike/MBW.Utilities.DI.Named/actions/workflows/dotnet.yml/badge.svg)](https://github.com/LordMike/MBW.Utilities.DI.Named/actions/workflows/dotnet.yml) [![NuGet](https://img.shields.io/nuget/v/MBW.Utilities.DI.Named.svg)](https://www.nuget.org/packages/MBW.Utilities.DI.Named) [![GHPackages](https://img.shields.io/badge/package-alpha-green)](https://github.com/LordMike/MBW.Utilities.DI.Named/packages/690356)

Named services implementation for `Microsoft.Extensions.DependencyInjection`

> [!TIP]
> In .NET 8.0, Microsoft introduced ["Keyed Services"](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-8.0#keyed-services) which may cover what you need. Do check that out before you use this library, as you may be able to achieve your task without additional assemblies.

## Features

You can add named services to a `ServiceCollection`, and later retrieve them. The library supports:

* Adding the same service under two different names
* Adding Transient, Scoped and Singleton services
* Disposals of services when the ServiceProvider is disposed
* Adding multiple services and retrieving them with `GetServices(name)`
* Ability to retrieve all named services of `T` by `GetNamedServices()`

## What you can't

You cannot inject named services into instances. The only way to use these named services, is by using the `IServiceProvider` and calling the appropriate extension methods on that.

### How to use

Add services as you normally would, and there'll generally be an overload of `Add*()` that takes a name. Retrieve services using the similar appropriate overloads that also take a name.

Adding services:

* `AddSingleton(string name)`
* `AddSingleton(string name)`
* `AddSingleton(string name, TService instance)`
* `AddSingleton(string name, Func factory)`
* `AddScoped(string name)`
* `AddScoped(string name)`
* `AddScoped(string name, TService instance)`
* `AddScoped(string name, Func factory)`
* `AddTransient(string name)`
* `AddTransient(string name)`
* `AddTransient(string name, TService instance)`
* `AddTransient(string name, Func factory)`
* `TryAddSingleton(string name, TService instance)`
* `TryAddSingleton(string name, Func factory)`
* `TryAddSingleton(string name)`
* `TryAddSingleton(string name)`
* `TryAddScoped(string name, TService instance)`
* `TryAddScoped(string name, Func factory)`
* `TryAddScoped(string name)`
* `TryAddScoped(string name)`
* `TryAddTransient(string name, TService instance)`
* `TryAddTransient(string name, Func factory)`
* `TryAddTransient(string name)`
* `TryAddTransient(string name)`

Retrieving services from a `ServiceProvider`:

* `T GetService(this IServiceProvider provider, string name)`
* `IEnumerable<(string name, T service)> GetNamedServices(this IServiceProvider provider)`
* `T GetRequiredService(this IServiceProvider provider, string name)`
* `IEnumerable GetServices(this IServiceProvider provider, string name)`