Skip to content

bnayae/Bnaya.Extensions.DependencyInjection

Repository files navigation

Dependency Injection Extensions

Build & Deploy NuGet

NuGet

Keyed Dependency Injection using .NET

You can register your components as:

builder.Services.AddKeyedSingleton<IFunctionality, AFunctionality>("A");
builder.Services.AddKeyedSingleton<IFunctionality, BFunctionality>("B");

And use it as follow:

[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
    private readonly IKeyed<IFunctionality> _selector;

    public TestController(IKeyed<IFunctionality> selector)
    {
        _selector = selector;
    }

    [HttpGet("a")]
    public string GetA()
    {
        return _selector["A"].Id; // throw if "A" not registered
    }

    [HttpGet("b")]
    public string GetB()
    {
        if(_selector.TryGet("B", out IFunctionality val))
            return val.Id;
        return "Not registered";
    }
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages