你在你的应用程序应用IoC容器了吗,你是否希望不依赖于某个具体的IoC,微软的模式与实践团队在Codeplex上发布的Common Service Locator。Common Service Locator 类库包含应用程序和框架开发者引用Service location共享的接口。这个类库提供了在IOC容器和Service locators之上抽象。使用这个类库允许一个应用程序在没有强引用依赖下间接的访问的能力。它所定义的接口非常简单:{

http://www.cnblogs.com/shanyou/archive/2008/12/27/1363785.html

Unity Adapter下载:

http://commonservicelocator.codeplex.com/wikipage?title=Unity%20Adapter&referringTitle=Home

UnityServiceLocator.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Practices.ServiceLocation;
  4.  
  5. namespace Microsoft.Practices.Unity.ServiceLocatorAdapter
  6. {
  7. public class UnityServiceLocator : ServiceLocatorImplBase
  8. {
  9. private IUnityContainer container;
  10.  
  11. public UnityServiceLocator(IUnityContainer container)
  12. {
  13. this.container = container;
  14. }
  15.  
  16. /// <summary>
  17. /// When implemented by inheriting classes, this method will do the actual work of resolving
  18. /// the requested service instance.
  19. /// </summary>
  20. /// <param name="serviceType">Type of instance requested.</param>
  21. /// <param name="key">Name of registered service you want. May be null.</param>
  22. /// <returns>
  23. /// The requested service instance.
  24. /// </returns>
  25. protected override object DoGetInstance(Type serviceType, string key)
  26. {
  27. return container.Resolve(serviceType, key);
  28. }
  29.  
  30. /// <summary>
  31. /// When implemented by inheriting classes, this method will do the actual work of
  32. /// resolving all the requested service instances.
  33. /// </summary>
  34. /// <param name="serviceType">Type of service requested.</param>
  35. /// <returns>
  36. /// Sequence of service instance objects.
  37. /// </returns>
  38. protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
  39. {
  40. return container.ResolveAll(serviceType);
  41. }
  42. }
  43. }

ServiceLocatorImplBase

  1. #region 程序集 Microsoft.Practices.ServiceLocation.dll, v2.0.50727
  2. \CommonServiceLocator.UnityAdapter\Lib\Microsoft.Practices.ServiceLocation.dll
  3. #endregion
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7.  
  8. namespace Microsoft.Practices.ServiceLocation
  9. {
  10. // 摘要:
  11. // This class is a helper that provides a default implementation for most of
  12. // the methods of Microsoft.Practices.ServiceLocation.IServiceLocator.
  13. public abstract class ServiceLocatorImplBase : IServiceLocator, IServiceProvider
  14. {
  15. protected ServiceLocatorImplBase();
  16.  
  17. // 摘要:
  18. // When implemented by inheriting classes, this method will do the actual work
  19. // of resolving all the requested service instances.
  20. //
  21. // 参数:
  22. // serviceType:
  23. // Type of service requested.
  24. //
  25. // 返回结果:
  26. // Sequence of service instance objects.
  27. protected abstract IEnumerable<object> DoGetAllInstances(Type serviceType);
  28. //
  29. // 摘要:
  30. // When implemented by inheriting classes, this method will do the actual work
  31. // of resolving the requested service instance.
  32. //
  33. // 参数:
  34. // serviceType:
  35. // Type of instance requested.
  36. //
  37. // key:
  38. // Name of registered service you want. May be null.
  39. //
  40. // 返回结果:
  41. // The requested service instance.
  42. protected abstract object DoGetInstance(Type serviceType, string key);
  43. //
  44. // 摘要:
  45. // Format the exception message for use in an Microsoft.Practices.ServiceLocation.ActivationException
  46. // that occurs while resolving multiple service instances.
  47. //
  48. // 参数:
  49. // actualException:
  50. // The actual exception thrown by the implementation.
  51. //
  52. // serviceType:
  53. // Type of service requested.
  54. //
  55. // 返回结果:
  56. // The formatted exception message string.
  57. protected virtual string FormatActivateAllExceptionMessage(Exception actualException, Type serviceType);
  58. //
  59. // 摘要:
  60. // Format the exception message for use in an Microsoft.Practices.ServiceLocation.ActivationException
  61. // that occurs while resolving a single service.
  62. //
  63. // 参数:
  64. // actualException:
  65. // The actual exception thrown by the implementation.
  66. //
  67. // serviceType:
  68. // Type of service requested.
  69. //
  70. // key:
  71. // Name requested.
  72. //
  73. // 返回结果:
  74. // The formatted exception message string.
  75. protected virtual string FormatActivationExceptionMessage(Exception actualException, Type serviceType, string key);
  76. //
  77. // 摘要:
  78. // Get all instances of the given TService currently registered in the container.
  79. //
  80. // 类型参数:
  81. // TService:
  82. // Type of object requested.
  83. //
  84. // 返回结果:
  85. // A sequence of instances of the requested TService.
  86. //
  87. // 异常:
  88. // Microsoft.Practices.ServiceLocation.ActivationException:
  89. // if there is are errors resolving the service instance.
  90. public virtual IEnumerable<TService> GetAllInstances<TService>();
  91. //
  92. // 摘要:
  93. // Get all instances of the given serviceType currently registered in the container.
  94. //
  95. // 参数:
  96. // serviceType:
  97. // Type of object requested.
  98. //
  99. // 返回结果:
  100. // A sequence of instances of the requested serviceType.
  101. //
  102. // 异常:
  103. // Microsoft.Practices.ServiceLocation.ActivationException:
  104. // if there is are errors resolving the service instance.
  105. public virtual IEnumerable<object> GetAllInstances(Type serviceType);
  106. //
  107. // 摘要:
  108. // Get an instance of the given TService.
  109. //
  110. // 类型参数:
  111. // TService:
  112. // Type of object requested.
  113. //
  114. // 返回结果:
  115. // The requested service instance.
  116. //
  117. // 异常:
  118. // Microsoft.Practices.ServiceLocation.ActivationException:
  119. // if there is are errors resolving the service instance.
  120. public virtual TService GetInstance<TService>();
  121. //
  122. // 摘要:
  123. // Get an instance of the given named TService.
  124. //
  125. // 参数:
  126. // key:
  127. // Name the object was registered with.
  128. //
  129. // 类型参数:
  130. // TService:
  131. // Type of object requested.
  132. //
  133. // 返回结果:
  134. // The requested service instance.
  135. //
  136. // 异常:
  137. // Microsoft.Practices.ServiceLocation.ActivationException:
  138. // if there is are errors resolving the service instance.
  139. public virtual TService GetInstance<TService>(string key);
  140. //
  141. // 摘要:
  142. // Get an instance of the given serviceType.
  143. //
  144. // 参数:
  145. // serviceType:
  146. // Type of object requested.
  147. //
  148. // 返回结果:
  149. // The requested service instance.
  150. //
  151. // 异常:
  152. // Microsoft.Practices.ServiceLocation.ActivationException:
  153. // if there is an error resolving the service instance.
  154. public virtual object GetInstance(Type serviceType);
  155. //
  156. // 摘要:
  157. // Get an instance of the given named serviceType.
  158. //
  159. // 参数:
  160. // serviceType:
  161. // Type of object requested.
  162. //
  163. // key:
  164. // Name the object was registered with.
  165. //
  166. // 返回结果:
  167. // The requested service instance.
  168. //
  169. // 异常:
  170. // Microsoft.Practices.ServiceLocation.ActivationException:
  171. // if there is an error resolving the service instance.
  172. public virtual object GetInstance(Type serviceType, string key);
  173. //
  174. // 摘要:
  175. // Implementation of System.IServiceProvider.GetService(System.Type).
  176. //
  177. // 参数:
  178. // serviceType:
  179. // The requested service.
  180. //
  181. // 返回结果:
  182. // The requested object.
  183. //
  184. // 异常:
  185. // Microsoft.Practices.ServiceLocation.ActivationException:
  186. // if there is an error in resolving the service instance.
  187. public virtual object GetService(Type serviceType);
  188. }
  189. }

【IOC--Common Service Locator】不依赖于某个具体的IoC的更多相关文章

  1. Atitit。如何实现dip, di ,ioc ,Service Locator的区别于联系

    Atitit.如何实现dip, di ,ioc  ,Service Locator的区别于联系 1. Dip原则又来自于松耦合思想方向1 2. 要实现dip原则,有以下俩个模式1 3. Ioc和di的 ...

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

    本文内容 场景 目标 解决方案 实现细节 思考 相关模式 更多信息 参考资料 Common Service Locator 代码很简单,它一般不会单独使用,而是作为一个单件模式,与像 .net Uni ...

  3. autofac使用Common Serivce Locator跟随wcf,mvc,web api的实例控制

    autofac本身只提供了基本的ioc容器的功能 要想在mvc,wcf,web api中使用,除了autofac本身,还需要引入对应的包(点击对应连接可查看文档) 除此之外,使用Common Serv ...

  4. Microsoft实现的IOC DI之 Unity 、Service Locator、MEF

    这几个工具的站点 Microsoft Unity  http://unity.codeplex.com Service Locator http://commonservicelocator.code ...

  5. 服务定位器(Service Locator)

    服务定位器(Service Locator) 跟DI容器类似,引入Service Locator目的也在于解耦.有许多成熟的设计模式也可用于解耦,但在Web应用上, Service Locator绝对 ...

  6. 【转】Understanding Inversion of Control, Dependency Injection and Service Locator Print

    原文:https://www.dotnettricks.com/learn/dependencyinjection/understanding-inversion-of-control-depende ...

  7. 依赖注入与Service Locator

    为什么需要依赖注入? ServiceUser是组件,在编写者之外的环境内被使用,且使用者不能改变其源代码. ServiceProvider是服务,其类似于ServiceUser,都要被其他应用使用,不 ...

  8. PHP中应用Service Locator服务定位及单例模式

    单例模式将一个对象实例化后,放在静态变量中,供程序调用. 服务定位(ServiceLocator)就是对象工场Factory,调用者对象直接调用Service Locator,与被调用对象减轻了依赖关 ...

  9. Service Locator 模式

    什么是Service Locator 模式? 服务定位模式(Service Locator Pattern)是一种软件开发中的设计模式,通过应用强大的抽象层,可对涉及尝试获取一个服务的过程进行封装.该 ...

随机推荐

  1. 跟着上一个tcpServer 一起来的

    具体功能就是通过反射为实体类赋值 public class httpParse<T> where T : new()    {        /// <summary>     ...

  2. 生成器generator

    生成器generator 定义:一个函数调用时返回一个迭代器,那这个函数就叫做生成器(generator),如果函数中包含yield语法,那这个函数就会变成生成器 代码: def cash_out(a ...

  3. BeanDefinition的Resource定位

    1.以编程的方式使用DefaultListableBeanFactory时,首先定义一个Resource来定位容器使用的BeanDefiniton.这时使用的是ClassPathResource,这意 ...

  4. Integer类的装箱和拆箱到底是怎样实现的?

    先解释一下装箱和拆箱: 装箱就是  自动将基本数据类型转换为包装器类型:拆箱就是  自动将包装器类型转换为基本数据类型. 下表是基本数据类型对应的包装器类型: int(4字节) Integer byt ...

  5. 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法

    $x_pi = 3.14159265358979324 * 3000.0 / 180.0; //火星坐标系 (GCJ-02)转百度坐标系 (BD-09)算法 function bd_encrypt($ ...

  6. mount.nfs: access denied by server while mounting localhost:/home/xuwq/minilinux/system

    在执行命令如下: mount -t nfs localhost:/home/xuwq/minilinux/system /mnt 出现的错误: mount.nfs: access denied by ...

  7. IntelliJ IDEA+Tomcat+Nginx运行git项目

    1.克隆Git项目到本地 (1)设置Git工具路径:file>settings>Version Control>Git (2)设置GitHub账户:file>settings& ...

  8. Mysql支持中文全文检索的插件mysqlcft-应用中的问题

    MySQL目前版本的全文检索没有对中文很好的支持,但可以通过安装mysqlcft插件来实现,具体的安装使用方法:http://blog.s135.com/post/356/ mysqlcft的官方网站 ...

  9. Geoserver 相关学习

    参考资料: http://geoserver.org/ http://docs.geoserver.org/ 相关文档 http://docs.geoserver.org/stable/en/user ...

  10. 一款jQuery立体感动态下拉导航菜单特效

    一款jQuery立体感动态下拉导航菜单特效,鼠标经过,在菜单栏上方下拉出一个背景图片,效果十分不错的一款jquery特效. 对IE6都是兼容的,希望大家好好研究研究. 适用浏览器:IE6.IE7.IE ...