.NET:Microsoft.Practices.ServiceLocation 之 动态单例模式
2013-05-06 12:30
190 查看
背景
框架开发中,经常会用到“单例模式”,但是传统的单例模式不支持多态和运行时变化,在关注测试的今天,这种模式是不可行的。为了应对这种情况,微软又提供了另外一种模式,暂且将其称为“动态单例模式”。 我也想统一我的框架对单例的使用模式,因此就写了这篇文章。Microsoft.Practices.ServiceLocation 核心代码
看完代码,如何使用这种模式就不用我多介绍了。
IServiceLocatorView Code
namespace Microsoft.Practices.ServiceLocation { /// <summary> /// This class provides the ambient container for this application. If your /// framework defines such an ambient container, use ServiceLocator.Current /// to get it. /// </summary> public static class ServiceLocator { private static ServiceLocatorProvider currentProvider; /// <summary> /// The current ambient container. /// </summary> public static IServiceLocator Current { get { return currentProvider(); } } /// <summary> /// Set the delegate that is used to retrieve the current container. /// </summary> /// <param name="newProvider">Delegate that, when called, will return /// the current ambient container.</param> public static void SetLocatorProvider(ServiceLocatorProvider newProvider) { currentProvider = newProvider; } } }
动态单例模式的优点
支持多态。运行时可变。
支持其它级别范围的单例,如:请求级、线程级和会话级等。
支持对象池。
备注
Microsoft.Practices.ServiceLocation下载地址:http://commonservicelocator.codeplex.com/。相关文章推荐
- Microsoft.Practices.ServiceLocation 之 动态单例模式
- 一起谈.NET技术,ASP.NET MVC3 Service Location
- 实现虚拟模式的动态数据加载Windows窗体DataGridView控件 .net 4.5 (一)
- 基于 .net 2.0 的 Microsoft.Practices.EnterpriseLibrary.Data(2006年1月版)
- .NET 动态调用WebService + WSE + UsernameToken
- 【ProgrammingMicrosoftAzureServiceFabric】第四章: Actor模式
- WCF学习问题之“net.tcp://localhost/service/”不支持正在使用的 .Net 组帧模式。有关详细信息,请参见服务器日志。
- Microsoft模式和实践:模式篇(Microsoft Patterns & Practices:
- .NET:动态代理的 “5 + 1” 模式
- [Silverlight入门系列]Prism中TreeView真正实现MVVM模式和Expanded发生时异步动态加载子节点(WCFRiaService)
- .net 的动态语言实现DCI架构模式的角色混入
- JAVA代理模式与动态代理模式
- Java框架--注解模式:@Repository @Service @Controller @Autowired
- Microsoft发布.NET架构指南草案
- 【.net 深呼吸】细说CodeDom(9):动态编译
- .net windows service开发与安装
- 代理模式【介绍、静态代理、动态代理、入门、应用】
- .Net 4.0 之 Dynamic 动态类型
- 静态代理与动态代理模式
- 在Microsoft Visual Studio 2005上安装.net 3.0开发环境(含开发环境下载地址)