开发者

Winforms MVP with Castle Windsor - DI for subforms?

开发者 https://www.devze.com 2022-12-26 17:28 出处:网络
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'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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号