views:

829

answers:

4

I'm starting a new Winforms app, and I intend to use an IoC/DI framework (probably Ninject, but i'm also thinking about StructureMap and LinFu).

It seems like nearly everyone who is using IoC/DI is doing so in a web based environment and have found virtually nothing on using Winforms with it.

I'd like to know if anyone is using IoC/DI with Winforms and what approaches you used to deal with Winforms related issues (for instance, how do you make the container available in various parts of the app, do you use the framework to instantiate your forms, etc..)

If anyone knows of any open source Winforms based projects that use IoC/DI (doesn't matter which framework, I should be able to translate concepts) I would like links to those as well.

EDIT:

Are people just not writing Smart Clients anymore?

EDIT:

If you could point me to some real-world code that uses IoC/DI in a Winforms or even console type application (ie, something that is not Web based) I'd appreciate it.

EDIT:

I've been using Ninject and discovered that Ninject will happily inject an instance of it's common kernel interface if you specify an IKernel constructor parameter. This has been working out pretty well, but I'd still like to hear other approaches people use.

+4  A: 

The Microsoft patterns and practices team which maintains the Unity injection container also created the Smart Client - Composite UI Application Block for winforms, which I believe uses Unity. I know the new WPF version called Composite Client Application Guidance (codename Prism) uses Unity

bendewey
I don't really see anything about Unity there... i'll dig a little deeper, but I think CUIAB predates Unity.
Mystere Man
+3  A: 

I just recently started writing a new WinForms application from scratch using StructureMap for IoC. I've previously looked at SCSF and CAB, but found those overly complex.

I wrote some fluent APIs on top of StructureMaps registry so our modules can register commands, presenters and views.

From my experience it has been worth the effort in all regards, I would never want to write a WinForms app without using these tools and the highly structured modular approach again.

__grover
Can you provide some examples of where you see IoC/DI as a win? I'm just getting into this, so I don't fully understand what you mean.
Mystere Man
Ok, our app is using IoC, MVP, DDD, TDD and fluent interfaces everywhere. Even though it has become already a pretty complex beast I'm very confident in its quality. Using IoC we can pull out every single piece of the app and test it in isolation. We've setup TeamCity to run tests on every commit.
__grover
Using IoC we were able to react on customer demands easily, by switching service implementations. An example: We switched DDD repositories on demand, we changed UI behavior by switching presenters. No more 'if app in mode x then do y else z' crap. Even wiring different UI behavior helped too.
__grover
+1  A: 

I've written an application for displaying maps from OpenStreetMap vector data (http://wiki.openstreetmap.org/index.php/Kosmos). I developed my own simple MVP framework which uses Windsor Castle as an IoC container. You can download the source code if you're interested, it's currently BSD-licensed.

Right now I'm (slowly) working on a redesign of this framework to be able to generate forms dynamically (instead of using the visual designer). I'm toying with the idea of introducing some kind of a fluent interface for building forms and controls, but I haven't yet come up with an elegant solution for this.

Igor Brejc
A: 

If you just want an Inversion of Control container and want to get a little lower level than Unity, check out ObjectBuilder from the Microsoft Patterns and Practices group. Unity is an abstraction layer on top of ObjectBuilder.

Travis Heseman