IServiceLocator 接口是什么?
据我了解, IServiceLocator 是一个抽象实际 IoC 容器的接口?我问的是与 Prism 相关的问题,我试图用 Prism 替换 Unity,并且我看到 Prism 类依赖于 IServiceLocator。有人可以澄清一下该接口的作用以及何时使用它吗?
还有;什么是通用服务定位器,这在使用 IServiceLocator 时会有帮助吗?
From what I understand IServiceLocator is an interface to abstract the actual IoC container away? I'm asking with relation to Prism where I'm trying to replace Unity with Prism, and I see Prism-classes relying on IServiceLocator. Could someone please clarify the role of the interface and when it is used?
And also; what is the Common Service Locator, and will this be helpful when working with IServiceLocator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IServiceLocator 是服务定位器的抽象。 IoC 容器是服务定位器的超集,因此它们可以用 IServiceLocator 表示。然而,服务定位器模式比纯依赖注入模式更具侵入性,因此您应该尽可能选择后者。
CommonServiceLocator 提供 IServiceLocator 接口和该接口的抽象实现来定义公共行为并帮助 IoC 容器定义其 IServiceLocator 适配器(每个主要的 IoC 容器都提供这样的适配器)。
另请参阅:
IServiceLocator is an abstraction of a service locator. IoC containers are kind of supersets of service locators so they can be represented by IServiceLocator. However, the service locator pattern is more intrusive than the pure dependency injection pattern, therefore you should try to favor the latter whenever possible.
The CommonServiceLocator provides the IServiceLocator interface and an abstract implementation of this interface to define common behavior and help IoC containers define their IServiceLocator adapter (every major IoC container provides such an adapter).
See also: