《Prism 5.0源码走读》Service Locator Pattern_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 《Prism 5.0源码走读》Service Locator Pattern

《Prism 5.0源码走读》Service Locator Pattern

 2014/8/15 2:56:08  萝卜根  程序员俱乐部  我要评论(0)
  • 摘要:在PrismBootstrapper里面取实例的时候使用ServiceLocator模式,使用的是CommonServiceLocator库(http://commonservicelocator.codeplex.com/)。ServiceLocation定义了IServiceLocator及其基本实现ServiceLocatorImplBase;定义了IServiceLocator委托ServiceLocatorProvider;定义了ActivationException
  • 标签:Service 源码

在Prism Bootstrapper里面取实例的时候使用 ServiceLocator模式,使用的是CommonServiceLocator库 (http://commonservicelocator.codeplex.com/)。

ServiceLocation定义了IServiceLocator及其基本实现ServiceLocatorImplBase;定义了IServiceLocator委托ServiceLocatorProvider;定义了ActivationException;还有个静态类ServiceLocator。

 Prism ServiceLocator的设置和使用

1. Bootstrapper定义了抽象方法去设置ServiceLocator

  protected abstract void ConfigureServiceLocator();

UnityBootstrapper提供了使用Unity Container的实现:

UnityServiceLocatorAdapter:

public class UnityServiceLocatorAdapter : ServiceLocatorImplBase
    {

设置Unity Container到ServiceLocator:

        protected override void ConfigureServiceLocator()
        {
            ServiceLocator.SetLocatorProvider(() => this.Container.Resolve<IServiceLocator>());
        }

2. 使用ServiceLocator:

        /// <summary>
        /// Initializes the modules. May be overwritten in a derived class to use a custom Modules Catalog
        /// </summary>
        protected virtual void InitializeModules()
        {
            IModuleManager manager = ServiceLocator.Current.GetInstance<IModuleManager>();
            manager.Run();
        }

 

发表评论
用户名: 匿名