JoeDevSharp.WinForms.Extensions.DependencyInjection
1.0.1
dotnet add package JoeDevSharp.WinForms.Extensions.DependencyInjection --version 1.0.1
NuGet\Install-Package JoeDevSharp.WinForms.Extensions.DependencyInjection -Version 1.0.1
<PackageReference Include="JoeDevSharp.WinForms.Extensions.DependencyInjection" Version="1.0.1" />
<PackageVersion Include="JoeDevSharp.WinForms.Extensions.DependencyInjection" Version="1.0.1" />
<PackageReference Include="JoeDevSharp.WinForms.Extensions.DependencyInjection" />
paket add JoeDevSharp.WinForms.Extensions.DependencyInjection --version 1.0.1
#r "nuget: JoeDevSharp.WinForms.Extensions.DependencyInjection, 1.0.1"
#:package JoeDevSharp.WinForms.Extensions.DependencyInjection@1.0.1
#addin nuget:?package=JoeDevSharp.WinForms.Extensions.DependencyInjection&version=1.0.1
#tool nuget:?package=JoeDevSharp.WinForms.Extensions.DependencyInjection&version=1.0.1
WinForms.Extensions.DependencyInjection
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.
Key Features
- Simplified DI container setup with native support for WinForms.
- Scoped form creation for automatic lifecycle and resource management.
- Dependency injection via constructor or
[Inject]
-marked properties. IInjectable
interface for post-injection initialization.- Global singleton access to the service provider.
- Enables clean, decoupled, and testable architecture.
- Seamless integration with companion frameworks
Reactive
andRouteManager
.
Installation
Add the package via NuGet or build from source:
dotnet add package WinForms.Extensions.DependencyInjection
Quick Start
Setup in Program.cs
using 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>());
Creating scoped forms
var formFactory = provider.GetRequiredService<FormFactory>();
var settingsForm = formFactory.CreateScopedForm<SettingsForm>();
settingsForm.Show();
Property injection with [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
}
}
Public API
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.
Benefits
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.
Repository and Contribution
https://github.com/JoeDevSharp/WinForms.Extensions.DependencyInjection
Contributions, issues, and pull requests are welcome.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0-windows7.0 is compatible. net9.0-windows was computed. net10.0-windows was computed. |
-
net8.0-windows7.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.6)
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.1 | 108 | 6/21/2025 |