Skip to content

Latest commit

 

History

History
30 lines (20 loc) · 1002 Bytes

File metadata and controls

30 lines (20 loc) · 1002 Bytes

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