StructureMap with Windows Forms

I'm used to working with StructureMap with web applications ... but now I am working on a Windows Forms project and I would like to use it, but I cannot configure it.

On the Internet, I have a bootloader class that is called on Application_Start on Global.asax, but I don’t know how to do the same on WinForms.

Thanks!

+4
source share
3 answers

You can initialize the container in the static main method that launches your application. Then extract the instances of the form from the container so that you can enter any necessary dependencies. You can still put the initialization code in Bootstrapper.

static class Program { [STAThread] static void Main() { ObjectFactory.Initialize(...); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(ObjectFactory.GetInstance<Form1>()); } } 
+3
source

For a Winforms application, the main element that initializes the first form is the counting part of Application_Start.

When using ORM cards with web applications, you usually use the thumb rule to create a context / data session for each HTTP request. For a Winforms application, you usually use context for each operation or for each form.

+1
source

You would structure the bootstrap configuration and IoC in the same way (although I'm not sure how you included the form classes themselves, I did not work very well with WinForms). The only real difference you need is when / where the initializer is called. It just needs to be at the launch of the application. For web applications, you really call it from Application_Start. I think that in WinForms applications it will be in the OnLoad event of the main form .

If you have the main method anywhere (similar to a console application), this will work. This could be if the WinForms application was ported from a console application, for example.

0
source

All Articles