Windows Forms Integration Guide
Windows Forms was never designed with dependency injection in mind. Although it is possible to use constructor injection on the Form
classes that are manually created in the Main
method, code for the UserControl
instances that are used in our forms is generated and will therefore need a default constructor.
Instead of doing constructor injection, there are alternatives. The simplest thing is to store the container in the Program
class and let the default constructors of your form classes request the dependencies it needs..
The following code snippet is an example of how to register the Simple Injector
container in the Program
static class Program
private static Container container;
public static TService GetInstance<TService>() where TService : class
static void Main()
private static void Bootstrap()
// Create the container as usual.
container = new Container();
// Register your types, for instance:
// Register the Container class.
Program.container = container;
With this code in place, we can now write our Form
classes as follows:
public partial class Form1 : Form
private readonly IUserRepository userRepository;
private readonly IUserContext userContext;
this.userRepository = Program.GetInstance<IUserRepository>();
this.userContext = Program.GetInstance<IUserContext>();
private void button1_Click(object sender, EventArgs e)