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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JoeDevSharp.WinForms.Extensions.DependencyInjection" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JoeDevSharp.WinForms.Extensions.DependencyInjection" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="JoeDevSharp.WinForms.Extensions.DependencyInjection" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add JoeDevSharp.WinForms.Extensions.DependencyInjection --version 1.0.1
                    
#r "nuget: JoeDevSharp.WinForms.Extensions.DependencyInjection, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package JoeDevSharp.WinForms.Extensions.DependencyInjection@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=JoeDevSharp.WinForms.Extensions.DependencyInjection&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=JoeDevSharp.WinForms.Extensions.DependencyInjection&version=1.0.1
                    
Install as a Cake Tool

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 and RouteManager.

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 Compatible and additional computed target framework versions.
.NET net8.0-windows7.0 is compatible.  net9.0-windows was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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