WPF DI 服务定位器
因此,我正在寻求一些说明,以了解如何从我的应用程序中删除服务定位器。
我有一个 ViewManagerService ,负责了解哪个视图处于活动状态、哪些视图处于打开状态并创建新视图。
目前,我的 ViewModel 通过构造函数注入获得 IViewManagerService 注入。这些 ViewModel 公开 ICommand,当调用这些 ICommand 时,可以调用
viewManager.Transition("MyCoolView", somePrimaryKey);
ViewManagerService,然后使用服务定位器通过键“MyCoolView”查找并实例化新视图。使用键字符串的原因是这样我可以将视图与视图模型解耦。我想保持 ViewManagerService 足够通用,以便我可以将它用于其他应用程序,所以我不希望它依赖于特定的 IAbstractFactory 接口。
有什么提示/建议吗?
So I'm looking for some clarification how it would be possible to remove the service locator from my application.
I have a ViewManagerService that is responsible for knowing which view is active, which views are open and creating a new view.
Currently my ViewModels get an IViewManagerService injected into them via constructor injection. These ViewModels expose ICommands that when invoked can then make a call to
viewManager.Transition("MyCoolView", somePrimaryKey);
The ViewManagerService then uses a service locator to look up and instantiate a new view with the key "MyCoolView". The reason for using a key string is so I can decouple the View from the ViewModels. I would like to keep the ViewManagerService generic enough so I can use it for other apps, so I don't want it to depend on a specific IAbstractFactory interface.
Any tips/suggestions ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 WPF 的数据模板引擎完全摆脱魔术字符串。最好的方法是使用 MVVM 模式。它与 DI 正交,但却是一个很好的推动者。
一旦完成这种转换,您就可以拥有纯 DI 架构,而不必依赖 服务定位器反模式。
You can get rid of magic strings completely by using WPF's data templating engine. The best way to do that is to use the MVVM pattern. It's orthogonal to DI, but a great enabler.
Once you've made that transition, you can have a pure DI architecture without having to rely on the Service Locator anti-pattern.