Package Description
$ dotnet add package AfterWireThe AfterWire library provides a set of extensions for IServiceCollection to simplify the registration of services that can happen after the ServiceProvider is built. It integrates seamlessly with Microsoft.Extensions.DependencyInjection.
Add the AfterWire Nuget to your solution and reference it in your application.
To use AfterWire, call the AddAfterWire extension method on your IServiceCollection:
IServiceCollection services = new ServiceCollection();
services.AddAfterWire();
services.AddAfterWireTransient<IEmployee, Employee>();
IAfterWireServiceProvider afterWireServiceProvider = serviceProvider.GetRequiredService<IAfterWireServiceProvider>();
afterWireServiceProvider.AddTransient<IShift, ShiftZero>("0");
afterWireServiceProvider.AddTransient<IShift, ShiftRegular>("1");
Use the IAfterWireServiceProvider or IAfterWireFactory<T> to resolve services, including keyed services:
IEmployee employee = serviceProvider.GetRequiredService<IEmployee>();
//Using Factory
IAfterWireFactory<IShift> shiftFactory = serviceProvider.GetRequiredService<IAfterWireFactory<IShift>>();
IShift shift = shiftFactory.GetKeyedRequiredService("1");
IAfterWireFactory is a useful way in code to create new instances of objects in places where you are creating objects based on a collection or user interaction.
One large advantage of using IAfterWireFactory is that you can avoid the Service Locator anti-pattern and also have your constructor show the true dependencies.
//Using Factory
IAfterWireFactory<IShift> shiftFactory = serviceProvider.GetRequiredService<IAfterWireFactory<IShift>>();
IShift shift = shiftFactory.GetKeyedRequiredService("1"); //1 is the shift. This could be something entered in by the user
Here is a complete example from the AfterWireSamples.Program.cs file:
using AfterWire;
using AfterWireSamples;
using Microsoft.Extensions.DependencyInjection;
IServiceCollection services = new ServiceCollection();
services.AddAfterWire();
services.AddAfterWireTransient<IEmployee, Employee>();
IServiceProvider serviceProvider = services.BuildServiceProvider();
IAfterWireServiceProvider afterWireServiceProvider = serviceProvider.GetRequiredService<IAfterWireServiceProvider>();
IAfterWireFactory<IShift> shiftFactory = serviceProvider.GetRequiredService<IAfterWireFactory<IShift>>();
afterWireServiceProvider.AddTransient<IShift, ShiftZero>("0");
afterWireServiceProvider.AddTransient<IShift, ShiftRegular>("1");
IEmployee test = serviceProvider.GetRequiredService<IEmployee>();
test.Name = "Kevin";
Console.WriteLine(shiftFactory.GetKeyedRequiredService("1").StartTime);
Console.WriteLine(test.Name);
This project is licensed under the MIT License.