码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-05-07 17:50:49      阅读:352      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

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

bubuko.com,布布扣
 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 }
bubuko.com,布布扣

 

 ServiceLocatorImplBase 

bubuko.com,布布扣
#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);
    }
}
bubuko.com,布布扣

 

 

 

【IOC--Common Service Locator】不依赖于某个具体的IoC,布布扣,bubuko.com

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

标签:des   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/easy5weikai/p/3709960.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!