.net - 什么是 IServiceLocator 接口(interface)?

标签 .net ioc-container iservicelocator

据我了解,IServiceLocator 是一个将实际 IoC 容器抽象出来的接口(interface)?我正在询问与 Prism 的关系,我正在尝试 replace Unity with Prism ,我看到 Prism 类依赖于 IServiceLocator。有人可以澄清接口(interface)的作用以及何时使用它吗?

并且;什么是Common Service Locator ,这在使用 IServiceLocator 时会有所帮助吗?

最佳答案

IServiceLocator 是服务定位器的抽象。 IoC 容器是服务定位器的一种超集,因此它们可以由 IServiceLocator 表示。但是,服务定位器模式比纯依赖注入(inject)模式更具侵入性,因此您应该尽可能倾向于后者。

CommonServiceLocator 提供 IServiceLocator 接口(interface)和该接口(interface)的抽象实现来定义常见行为并帮助 IoC 容器定义它们的 IServiceLocator 适配器(每个主要的 IoC 容器都提供这样的适配器)。

也可以看看:

  • http://martinfowler.com/articles/injection.html#ServiceLocatorVsDependencyInjection
  • http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html
  • 关于.net - 什么是 IServiceLocator 接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2621316/

    相关文章:

    c# - 无法检索项目元数据。确保这是一个基于MSBuild的.NET Core项目。 (迁移)

    .net - WCF @ServiceHost 调试 ="true"但 web.config 编译 ="false"

    php - 如何正确地将依赖项注入(inject) Laravel artisan 命令?

    c# - Unity 的自定义对象工厂扩展

    dependency-injection - 使用 IOC 框架绑定(bind)到多个具体实现?

    .net - 如何将我的 IServiceLocator 实现设置为 ServiceLocator.Current?

    c# - 使用 RestSharp 反序列化 JSON 时出现问题

    c# - 静态属性最终会出现在 gen2 中吗?