【IOC--Common Service Locator】不依赖于某个具体的IoC

时间:2020-12-13 05:08:40

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