Windows Forms Integration GuideΒΆ

Doing dependency injection in Windows Forms is easy, since Windows Forms does not lay any constraints on the constructors of your Form classes. You can therefore simply use constructor injection in your form classes and let the container resolve them.

The following code snippet is an example of how to register Simple Injector container in the Program class:

using System;
using System.Windows.Forms;
using SimpleInjector;

static class Program {
    private static Container container;

    static void Main() {


    private static void Bootstrap() {
        // Create the container as usual.
        container = new Container();

        // Register your types, for instance:
        container.RegisterSingle<IUserRepository, SqlUserRepository>();
        container.Register<IUserContext, WinFormsUserContext>();

        // Optionally verify the container.

        // Register the Container class.
        Program.container = container;

With this code in place, we can now write our Form and UserControl classes as follows:

public partial class Form1 : Form {
    private readonly IUserRepository userRepository;
    private readonly IUserContext userContext;

    public Form1(IUserRepository userRepository, IUserContext userContext) {
        this.userRepository = userRepository;
        this.userContext = userContext;


    private void button1_Click(object sender, EventArgs e) {
        if (this.userContext.IsAdministrator) {