Winforms MVP 与 Castle Windsor - 子表单的 DI?
我正在构建一个 winforms 应用程序,使用被动视图 MVP 和 Castle Windsor 作为 IoC 容器。我对依赖注入和 MVP 还是有点陌生,所以我正在寻找一些清晰度...
我有一个主窗体,其中包含许多用户控件,并且还会弹出其他对话框(例如登录、选项)等)根据需要。我的第一个问题是......我应该使用构造函数注入将这些其他视图的演示者获取到主视图中,还是应该返回到服务定位器类型模式? (有人告诉我这是一个大问题!)还是其他什么?
第二个问题...用户控件在“完成”时需要与主窗体进行通信(该状态的定义因控件而异)。有连接这些的标准方法吗?我在想也许只是在主要演示者和儿童演示者之间连接事件,但我不确定这是否是正确的想法。
我很感激任何帮助,似乎 winforms 中 MVP 和 IoC 的组合并没有完全记录。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先我要说的是,我已经有一段时间没有编写 WinForms 了,所以我的说法可能不完全准确。
如果您使用被动视图,您的视图应该没有依赖项,因此您不需要向其中注入任何内容。 Presenter 应由容器实例化,并通过 .ctor 将 IView 注入其中。通常避免使用服务定位器 - 这篇文章 描述了一些强大的替代方案。
为了进行通信,我将使用某种桥接接口,该接口将被注入到演示者中。我在这里很模糊,因为细节取决于您的场景的具体情况,以及您决定如何准确地划分逻辑。
一般来说,不要太关注 IoC 容器——它只是一个可以为你完成一些(大部分)工作的工具,但它不是一根魔杖——你应该能够在没有容器到位的情况下做同样的事情,因此,如果它分散了您的注意力,请在制作原型时随意将其从图片中删除。
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.