Winforms MVP with Castle Windsor - DI for subforms?
I am creating a winforms app using MVP with passive view and Castle Windsor as IoC container. I'm still a bit new to Dependency Injection and MVP, so I'm looking for some clarity ...
I have a main form that contains a number of custom controls and will also create other dialogs if needed (like Login, options, etc.). My first question is ... should I use constructor injection to force presenters for these other views in the main view, or should I fall back to the Service Locator type pattern? (I was told it was a big non!) Or something else?
Second question ... custom controls need to return to the main form when they are "complete" (the definition of this state depends on the control). Is there a standard way to connect them? I thought that maybe just passing events between the main speaker and the lead kids, but I'm not sure if that's the right mindset.
Any help would be appreciated, it looks like the mix of MVP and IoC in winforms is not well documented.
a source to share
Let me start by saying that I haven't written WinForms in a while, so I can't be completely accurate.
If you are using passive view, your views shouldn't have any dependencies, so you don't need to inject anything into them. The presenter must be instantiated by the container and the IView is injected into it via .ctor. As a rule of thumb, avoid the Service Locator - this post describes some powerful alternatives.
For feedback, I would use some kind of bridged interface that would be injected into the presenter. I'm vague because the details depend on the specifics of your scenario and how exactly you decided to split your logic.
Generally not too focused on the IoC container - it's just a tool that will do (most) of the work for you, but it's not a magic wand - you should be able to do the same without the container in place, so if that distracts your attention , feel free to remove it from the image when prototyping.
a source to share