星空网 > 软件开发 > ASP.net

.NET 服务器定位模式(Service Locator Pattern)

本文内容

  • 场景
  • 目标
  • 解决方案
  • 实现细节
  • 思考
  • 相关模式
  • 更多信息
  • 参考资料

下载 Demo

场景


你有一个类,该类依赖几个服务 Service(事实上,这些服务可以看做是另几个类),这些服务是在编译阶段指定具体类型的。在接下来的例子中,ClassA 在编译阶段依赖 ServiceA 和ServiceB。下图说明这个问题。

.NET 服务器定位模式(Service Locator Pattern)

这种情况有如下缺点:

  • 若替换或更新依赖的服务(或类),显然,必须修改源代码,并且重新编译解决方案;
  • 这些依赖的具体实现(依赖的服务的具体实现)必须在编译时可用;The concrete implementation of the dependencies must be available at compile time.
  • 你的类很难单独测试,因为它们直接引用了那些依赖。这意味着这些依赖不能用 stubs 或 mock 对象替换;
  • 你的类包含创建、查找和管理这些依赖的重复代码。

下面说明如何解决这个问题。

目标


使用服务器定位模式可以完成下面的任何一个目标:

  • 你想从那些依赖解耦你的类,这样那些依赖就可以被替换或更新,而不需要或很少修改你的类;
  • 你想写逻辑代码,而逻辑代码取决于那些依赖的类,但那些依赖类的具体实现在编译阶段是不知道的;
  • 你想在不使用那些依赖的情况下单独测试你的类;
  • 你不想在你的类中写查找和管理那些依赖类的代码;
  • 你想把你的应用程序划分成松散的耦合模块,这样就可以单独开发、测试、版本控制和部署。

解决方案


创建一个包含那些服务的引用和封装了定位他们的逻辑的服务定位器。在你的类中,使用服务定位器来获得服务的实例。下图说明了类何使用服务器定位器。

.NET 服务器定位模式(Service Locator Pattern)

服务器定位器模式不会描述如何实例化服务,它描述一个注册服务和定位服务的方法。通常情况下,服务定位器模式结合工厂模式(Factory pattern)和/或依赖注入模式(Dependency Injection pattern)。这种组合使得服务定位器创建服务的实例。

注意:

服务定位器可以定位一个服务,而无需知道它的具体类型。例如,它可能使用一个字符串密钥(string key)或服务接口类型(service interface type)。这样,你就可以替换依赖的具体实现,而无需修改类。

现实细节


SharePoint Guidance Library 提供了一个服务定位器的实现。SharePointServiceLocator 类提供了访问单件 IServiceLocator 实例并管理该实例,该类一个接口的默认实现——ActivatingServiceLocator,这个类可以创建和定位服务。

The Partner Portal application 展示了如何使用服务定位器注册和定位服务,如信息库(repositories),记录服务(logging services)和配置管理服务(configuration management services)。欲了解更多信息,请参阅SharePoint服务定位器。

The Partner Portal application shows how to use the Service Locator to register and locate services such as repositories, logging services, and configuration management services. 更多信息,参看 The SharePoint Service Locator。

思考


在使用服务定位器模式前,考虑下面几点:

  • There are more solution elements to manage.
  • You must write additional code that adds service references to the service locator before your objects can use it.
  • Your classes have a dependency on the service locator.
  • The source code is more complex and difficult to understand.
  • You can use configuration data to define run-time relationships.
  • You must provide implementations of the services. Because the Service Locator pattern decouples service consumers from service providers, it might be necessary to provide additional logic. This logic ensures that the service providers are installed and registered before service consumers try to locate them.

相关模式


下面模式与服务定位器模式有关:

    依赖注入(Dependency Injection)。这个模式跟服务定位器模式解决的同一个问题,只是使用了不同的方法。

    控制反转(Inversion of Control)。服务定位器是控制反转的一个特例。它反转了一个应用程序的传统控制流。它是一个被调用的对象,而不是控制处理的调用者。

更多信息


参考:

  • Inversion of Control and the Dependency Injection pattern on Martin Fowler's Web site
  • Service Locator on MSDN

关于服务定位器,更多资料,参看 The SharePoint Service Locator。

参考资料


  • Microsoft Developer Network - The Service Locator Pattern
  • The SharePoint Service Locator
  • 控制反转和依赖注入模式 by Martin Fowler
  • Service Locator
  • Common Service Locator library
  • Service Locator is an Anti-Pattern by Mark Seemann

 

下载 Demo




原标题:.NET 服务器定位模式(Service Locator Pattern)

关键词:.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

商务部回应美对华加征关税/机电文化等产品退税率大幅提高:https://www.ikjzd.com/articles/6273
亚马逊怎么索赔?天秤星索赔助手动动手就能找回18个月错算款!:https://www.ikjzd.com/articles/6276
“PETS Rock ”品牌被曝侵权,各位卖家注意啦!:https://www.ikjzd.com/articles/6277
17个创建完美的产品描述的新角度:https://www.ikjzd.com/articles/6279
人民币跨境支付系统升级:https://www.ikjzd.com/articles/628
亚马逊英国站多账号KYC审核的建议:https://www.ikjzd.com/articles/6289
抖音电商副总裁将赴TikTok电商任职:https://www.kjdsnews.com/a/1836490.html
抖音电商副总裁将赴TikTok电商任职:https://www.goluckyvip.com/news/187213.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流