You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

JoeDevSharp.WinForms.Extensions.DependencyInjection

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

JoeDevSharp.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.

1.0.1
Source
nugetNuGet
Version published
Maintainers
1
Created
Source

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.

Keywords

WinForms

FAQs

Package last updated on 21 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts