Skip to content

mustaddon/OverridableDependencyInjection

Repository files navigation

OverridableDependencyInjection NuGet version

Overridable injections for Microsoft.Extensions.DependencyInjection.

Example

var provider = new ServiceCollection()
    .AddTransient<IExampleService1, ExampleService1A>()
    
    // REQUIRED: Adds override capability for IExampleService1
    .AddOverridability(typeof(IExampleService1))

    .BuildServiceProvider();



// SCOPE
using (var scope = provider.CreateScope())
{
    // Override the service implementation for this scope
    scope.Override<IExampleService1, ExampleService1B>();

    // Testing the override
    var serviceInstance = scope.ServiceProvider.GetRequiredService<IExampleService1>();

    Console.WriteLine($"IExampleService1 overridden: {serviceInstance is ExampleService1B}");
}

Program.cs

About

Overridable injections for Microsoft.Extensions.DependencyInjection

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published