你在你的应用程序应用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

 using System;
using System.Collections.Generic;
using Microsoft.Practices.ServiceLocation; namespace Microsoft.Practices.Unity.ServiceLocatorAdapter
{
public class UnityServiceLocator : ServiceLocatorImplBase
{
private IUnityContainer container; public UnityServiceLocator(IUnityContainer container)
{
this.container = container;
} /// <summary>
/// When implemented by inheriting classes, this method will do the actual work of resolving
/// the requested service instance.
/// </summary>
/// <param name="serviceType">Type of instance requested.</param>
/// <param name="key">Name of registered service you want. May be null.</param>
/// <returns>
/// The requested service instance.
/// </returns>
protected override object DoGetInstance(Type serviceType, string key)
{
return container.Resolve(serviceType, key);
} /// <summary>
/// When implemented by inheriting classes, this method will do the actual work of
/// resolving all the requested service instances.
/// </summary>
/// <param name="serviceType">Type of service requested.</param>
/// <returns>
/// Sequence of service instance objects.
/// </returns>
protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
{
return container.ResolveAll(serviceType);
}
}
}

ServiceLocatorImplBase

#region 程序集 Microsoft.Practices.ServiceLocation.dll, v2.0.50727
\CommonServiceLocator.UnityAdapter\Lib\Microsoft.Practices.ServiceLocation.dll
#endregion using System;
using System.Collections.Generic; namespace Microsoft.Practices.ServiceLocation
{
// 摘要:
// This class is a helper that provides a default implementation for most of
// the methods of Microsoft.Practices.ServiceLocation.IServiceLocator.
public abstract class ServiceLocatorImplBase : IServiceLocator, IServiceProvider
{
protected ServiceLocatorImplBase(); // 摘要:
// When implemented by inheriting classes, this method will do the actual work
// of resolving all the requested service instances.
//
// 参数:
// serviceType:
// Type of service requested.
//
// 返回结果:
// Sequence of service instance objects.
protected abstract IEnumerable<object> DoGetAllInstances(Type serviceType);
//
// 摘要:
// When implemented by inheriting classes, this method will do the actual work
// of resolving the requested service instance.
//
// 参数:
// serviceType:
// Type of instance requested.
//
// key:
// Name of registered service you want. May be null.
//
// 返回结果:
// The requested service instance.
protected abstract object DoGetInstance(Type serviceType, string key);
//
// 摘要:
// Format the exception message for use in an Microsoft.Practices.ServiceLocation.ActivationException
// that occurs while resolving multiple service instances.
//
// 参数:
// actualException:
// The actual exception thrown by the implementation.
//
// serviceType:
// Type of service requested.
//
// 返回结果:
// The formatted exception message string.
protected virtual string FormatActivateAllExceptionMessage(Exception actualException, Type serviceType);
//
// 摘要:
// Format the exception message for use in an Microsoft.Practices.ServiceLocation.ActivationException
// that occurs while resolving a single service.
//
// 参数:
// actualException:
// The actual exception thrown by the implementation.
//
// serviceType:
// Type of service requested.
//
// key:
// Name requested.
//
// 返回结果:
// The formatted exception message string.
protected virtual string FormatActivationExceptionMessage(Exception actualException, Type serviceType, string key);
//
// 摘要:
// Get all instances of the given TService currently registered in the container.
//
// 类型参数:
// TService:
// Type of object requested.
//
// 返回结果:
// A sequence of instances of the requested TService.
//
// 异常:
// Microsoft.Practices.ServiceLocation.ActivationException:
// if there is are errors resolving the service instance.
public virtual IEnumerable<TService> GetAllInstances<TService>();
//
// 摘要:
// Get all instances of the given serviceType currently registered in the container.
//
// 参数:
// serviceType:
// Type of object requested.
//
// 返回结果:
// A sequence of instances of the requested serviceType.
//
// 异常:
// Microsoft.Practices.ServiceLocation.ActivationException:
// if there is are errors resolving the service instance.
public virtual IEnumerable<object> GetAllInstances(Type serviceType);
//
// 摘要:
// Get an instance of the given TService.
//
// 类型参数:
// TService:
// Type of object requested.
//
// 返回结果:
// The requested service instance.
//
// 异常:
// Microsoft.Practices.ServiceLocation.ActivationException:
// if there is are errors resolving the service instance.
public virtual TService GetInstance<TService>();
//
// 摘要:
// Get an instance of the given named TService.
//
// 参数:
// key:
// Name the object was registered with.
//
// 类型参数:
// TService:
// Type of object requested.
//
// 返回结果:
// The requested service instance.
//
// 异常:
// Microsoft.Practices.ServiceLocation.ActivationException:
// if there is are errors resolving the service instance.
public virtual TService GetInstance<TService>(string key);
//
// 摘要:
// Get an instance of the given serviceType.
//
// 参数:
// serviceType:
// Type of object requested.
//
// 返回结果:
// The requested service instance.
//
// 异常:
// Microsoft.Practices.ServiceLocation.ActivationException:
// if there is an error resolving the service instance.
public virtual object GetInstance(Type serviceType);
//
// 摘要:
// Get an instance of the given named serviceType.
//
// 参数:
// serviceType:
// Type of object requested.
//
// key:
// Name the object was registered with.
//
// 返回结果:
// The requested service instance.
//
// 异常:
// Microsoft.Practices.ServiceLocation.ActivationException:
// if there is an error resolving the service instance.
public virtual object GetInstance(Type serviceType, string key);
//
// 摘要:
// Implementation of System.IServiceProvider.GetService(System.Type).
//
// 参数:
// serviceType:
// The requested service.
//
// 返回结果:
// The requested object.
//
// 异常:
// Microsoft.Practices.ServiceLocation.ActivationException:
// if there is an error in resolving the service instance.
public virtual object GetService(Type serviceType);
}
}

【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. AMQ学习笔记 - 20. 使用Apache ActiveMQBrowser监控ActiveMQ

    概述 Apache ActiveMQBrowser可以用于查看AMQ中的消息.这里对其使用方法进行简单介绍. 使用介绍 1.下载并解压缩 下载地址:Apache ActiveMQBrowser,当前最 ...

  2. 8个超炫酷的纯CSS3动画及源码分享

    在现代网页中,我们已经越来越习惯使用大量的CSS3元素,而现在的浏览器也基本都支持CSS3,所以很多时候我们不妨思考一下是否可以用纯CSS3制作一些有趣或者实用的网页.本文要分享8个超炫酷的纯CSS3 ...

  3. 基于 ArcGIS Silverlight API开发的WebGIS应用程序的部署

    部署流程概述 在微软的iis服务器上部署基于ArcGIS  Silverlight API的应用程序,主要包括以下几个步骤: 1)(可选)部署GIS服务 如果需要将GIS服务也部署在Web服务器上,则 ...

  4. Lucene 3.0

    http://www.cnblogs.com/forfuture1978/archive/2010/02/22/1671487.html http://www.cnblogs.com/jiekzou/ ...

  5. 无法访问Fedora的samba共享

    配置好samba服务后,却发现windows无法访问.经过多次试验与fedora的防火墙有关系. 关闭防火墙: #service iptables stop 或清空规则: #iptables -F w ...

  6. 管道和FIFO

    pipe 子进程从终端读取一个文件名, 通过管道将文件名传递给父进程 父进程收到文件名后, 读取文件内容并通过管道传递给子进程 子进程接收到文件内容并输出到终端 #include <stdio. ...

  7. IE中出现 "Stack overflow at line" 错误的解决方法

    在做网站时遇到一个问题,网站用的以前的程序,在没有改过什么程序的情况下,页面总是提示Stack overflow at line 0的错误,而以前的网站都正常没有出现过这种情况,在网上找了一下解决办法 ...

  8. Java中的继承和多态

    1.  什么是继承,继承的特点? 子类继承父类的特征和行为,使得子类具有父类的各种属性和方法.或子类从父类继承方法,使得子类具有父类相同的行为. 特点:在继承关系中,父类更通用.子类更具体.父类具有更 ...

  9. Android中解析JSON形式的数据

    1.JSON(JavaScript Object Notation) 定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式, ...

  10. Python实战(1)

    此次实战完全按照Python教程 - 廖雪峰的官方网站进行 首先下载windows版本的Python2.7,附上下载链接http://www.python.org/ftp/python/2.7.6/p ...