AfterWire 1.0.0
dotnet add package AfterWire --version 1.0.0
NuGet\Install-Package AfterWire -Version 1.0.0
<PackageReference Include="AfterWire" Version="1.0.0" />
<PackageVersion Include="AfterWire" Version="1.0.0" />
<PackageReference Include="AfterWire" />
paket add AfterWire --version 1.0.0
#r "nuget: AfterWire, 1.0.0"
#:package AfterWire@1.0.0
#addin nuget:?package=AfterWire&version=1.0.0
#tool nuget:?package=AfterWire&version=1.0.0
AfterWire Dependency Injection Extensions
The 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
.
Features
- Dynamic Service Registration: Register services dynamically after ServiceProvider is already created.
- Factories: Allows for multiple object instantiation of registered classes using generics.
- Keyed Services: Support for keyed service registration and resolution.
- Scoped, Transient, and Singleton Lifetimes: Easily register services with different lifetimes.
Installation
Add the AfterWire
Nuget to your solution and reference it in your application.
Usage
Adding AfterWire to Your Service Collection
To use AfterWire
, call the AddAfterWire
extension method on your IServiceCollection
:
IServiceCollection services = new ServiceCollection();
services.AddAfterWire();
Registering Services
Registering before ServiceProvider is built.
services.AddAfterWireTransient<IEmployee, Employee>();
Registering after ServiceProvider is built.
IAfterWireServiceProvider afterWireServiceProvider = serviceProvider.GetRequiredService<IAfterWireServiceProvider>();
afterWireServiceProvider.AddTransient<IShift, ShiftZero>("0");
afterWireServiceProvider.AddTransient<IShift, ShiftRegular>("1");
Resolving Services
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");
Factories
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<T> 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
Full Example
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);
License
This project is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.0 | 213 | 6/9/2025 |
Initial Revision