Skip to content

weknow-network/Weknow-DI-Extensions

Repository files navigation

Dependency Injection Extensions

by WeKnow
Weknow.DI.Extensions: NuGet
Weknow.DI.Extensions.Contracts: NuGet
Prepare Build & Deploy 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;
    }

    [HttpGet("b")]
    public string GetB()
    {
        return _selector["B"].Id;
    }
}

Looking for other extensions?
Check the following

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages