English 中文(简体)
How to not pass around the container when using IoC in Winforms
原标题:

I m new to the world of IoC and having a problem with implementing it in a Winforms application. I have an extremely basic application Winform application that uses MVC, it is one controller that does all the work and a working dialog (obviously with a controller). So I load all my classes in to my IoC container in program.cs and create the main form controller using the container. But this is where I am having problems, I only want to create the working dialog controller when it s used and inside a using statement.

At first I passed in the container but I ve read this is bad practice and more over the container is a static and I want to unit test this class.

So how do you create classes in a unit test friendly way without passing in the container, I was considering the abstract factory pattern but that alone would solve my problem without using the IoC.

I m not using any famous framework, I borrowed a basic one from this blog post http://www.kenegozi.com/Blog/2008/01/17/its-my-turn-to-build-an-ioc-container-in-15-minutes-and-33-lines.aspx

How do I do this with IoC? Is this the wrong use for IoC?

最佳回答

Ken s post is very interesting, but you re at the point where it is worth learning more about the "production" IoC containers, as a few now support this scenario.

In Autofac for example, you can generate a factory as a delegate:

builder.RegisterGeneratedFactory<Func<IDialogController>>();

Then in your main form:

class MainForm ... {

  Func<IDialogController> _controllerFactory;

  public MainForm(Func<IDialogController> controllerFactory) { ... }

  void ShowDialog() {
    using (var controller = _controllerFactory())
    {
    }
  }

Autofac will fill in the controllerFactory constructor parameter at runtime. In your unit tests you can easily provide a lambda to the constructor instead.

问题回答

I generally just pass in an interface to a factory class.

The only reasonable solution I came around with is making your container Singleton. Some of the IoC frameworks do that for you, but you might have to roll out your own implementation of Singleton. Have a look at Jon Skeet s ideas.

Good luck with MVC in Winforms. It is a steep learning curve, that I am only beginning to ascend on.





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签