Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/lordmike/mbw.utilities.di.named
- Owner: LordMike
- License: mit
- Created: 2019-03-31T15:48:14.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-04-05T15:43:01.000Z (7 months ago)
- Last Synced: 2024-11-02T12:39:02.287Z (3 days ago)
- Language: C#
- Homepage:
- Size: 68.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
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)`