Advanced Dependency Injection (DI) integration tailored for Windows Forms applications. Built on top of `Microsoft.Extensions.DependencyInjection`, but adapted and extended to address WinForms-specific development needs.
License
—
Deps
1
Install Size
—
Vulns
✓ 0
Published
Jun 21, 2025
$ dotnet add package JoeDevSharp.WinForms.Extensions.DependencyInjectionAdvanced Dependency Injection (DI) integration tailored for Windows Forms applications. Built on top of Microsoft.Extensions.DependencyInjection, but adapted and extended to address WinForms-specific development needs.
[Inject]-marked properties.IInjectable interface for post-injection initialization.Reactive and RouteManager.Add the package via NuGet or build from source:
dotnet add package WinForms.Extensions.DependencyInjection
Program.csusing Microsoft.Extensions.DependencyInjection;
using WinForms.Extensions.DependencyInjection.Bootstrap;
var services = new ServiceCollection();
// Register your services and forms here
services.AddSingleton<IMainService, MainService>();
services.AddTransient<MainForm>();
services.AddSingleton<FormFactory>();
var provider = services.BuildWinFormsServiceProvider();
ServiceProviderGlobal.Instance.Initialize(provider);
Application.Run(provider.GetRequiredService<MainForm>());
var formFactory = provider.GetRequiredService<FormFactory>();
var settingsForm = formFactory.CreateScopedForm<SettingsForm>();
settingsForm.Show();
[Inject]public partial class MainForm : Form, IInjectable
{
[Inject]
public IMyService MyService { get; set; }
public MainForm()
{
InitializeComponent();
this.ResolveInjectedProperties(ServiceProviderGlobal.Instance.Provider);
}
public void OnInjected()
{
// Initialization logic after injection
}
}
ServiceProviderBuilder.BuildWinFormsServiceProvider(IServiceCollection): Builds the DI container.FormFactory.CreateScopedForm<T>(): Creates a form with its own scoped lifetime.[Inject]: Attribute to mark properties for injection.ResolveInjectedProperties(this object, IServiceProvider): Extension for automatic property injection.IInjectable: Interface for post-injection initialization callback.ServiceProviderGlobal.Instance: Singleton for global service provider access.This module addresses common DI challenges in WinForms by integrating naturally with the platform and respecting its paradigms. It simplifies transitioning to modern, maintainable architectures.
https://github.com/JoeDevSharp/WinForms.Extensions.DependencyInjection
Contributions, issues, and pull requests are welcome.