views:

254

answers:

1

I'm building a winforms app utilizing passive-view MVP and Castle Windsor as an IoC container. I'm still a little new to dependency injection and MVP, so I'm looking for some clarity...

I have a main form which contains a number of user controls, and also will bring up other dialogs (ex. Login, options, etc) as needed. My first question is...should I use constructor injection to get the presenters for these other views into the main view, or should I go back to a Service Locator-type pattern? (which I've been told is a big nono!) Or something else?

Second question...the user controls need to communicate back to the main form when they are "completed" (definition of that state varies based on the control). Is there a standard way of hooking these up? I was thinking perhaps just wiring up events between the main presenter and the child presenters, but I'm not sure if this is proper thinking.

I'd appreciate any help, it seems that the combination of MVP and IoC in winforms isn't exactly well-documented.

A: 

Let me start by saying I didn't write WinForms in quite some time so I may not be completely accurate.

If you're using passive view your views should have no dependencies, so you shouldn't need to inject anything into them. Presenter should be instantiated by the container and have IView injected into it via .ctor. Generally avoid Service Locator - this post describes some powerful alternatives.

For communication back, I'd use some kind of bridge-interface, that would be injected into the presenter. I'm vague here, because details depend on specifics of your scenario, and how exactly you decide to partition your logic.

Generally don't get too focused on IoC container - it's just a tool that will do some (big) part of work for you, but it's not a magic wand - you should be able to do the same thing without the container in place, so if it's distracting your focus, feel free to remove it from the picture when prototyping.

Krzysztof Koźmic
Thanks Krzysztof - I've actually realized that the presenters need to be communicating in both directions between each other, so I'm going to use the mediator pattern between them. Very interesting article on Service Locator alternatives, I will try to implement them as well!
Paul Kirby