早在Enterprise Library
5.0以前,就早已有了3个Common Service
Locator
.在那边假若你对Enterprise
Library已经有自然领会。
在Enterprise Library
5.0中,我们日常会选拔EnterpriseLibraryContainer.Current,实际上它是IServiceLocator.aspx)接口。例如下边包车型地铁代码:

   1:          [TestMethod]

   2:          public void TestGetExceptionManagerFromDefaultEntLibContainer()

   3:          {

   4:              var exceptionManager=

   5:              EnterpriseLibraryContainer.Current.

   6:                  GetInstance<Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager>();

   7:   

   8:              Assert.IsNotNull(exceptionManager);

   9:          }

大家从容器中取得一个ExceptionManager的对象.暗中同意也是利用是Unity.
大家得以看这么壹段CODE在EnterpriseLibraryContainer.aspx)类中:

   1:  public static IServiceLocator CreateDefaultContainer(IConfigurationSource configurationSource)

   2:  {

   3:      IUnityContainer container = new UnityContainer();

   4:      UnityContainerConfigurator configurator = new UnityContainerConfigurator(container);

   5:      ConfigureContainer(configurator, configurationSource);

   6:      return new UnityServiceLocator(container);

   7:  }

所以借使你使用xml配置文件来布局Unity,那么就要这样完结:

   1:          [TestMethod]

   2:          public void TestGetExceptionManagerFromServiceLocator()

   3:          {

   4:              // Create the container

   5:              var container = new UnityContainer();

   6:   

   7:              // Configurator will read Enterprise Library configuration 

   8:              // and set up the container

   9:              var configurator = new UnityContainerConfigurator(container);

  10:   

  11:              // Configuration source holds the new configuration we want to use 

  12:              // load this in your own code

  13:              var configSource = new FileConfigurationSource("EntLib.config.xml");

  14:   

  15:              // Configure the container

  16:              EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

  17:   

  18:              // Wrap in ServiceLocator

  19:              var locator = new UnityServiceLocator(container);

  20:   

  21:              // And set Enterprise Library to use it

  22:              EnterpriseLibraryContainer.Current = locator;

  23:   

  24:              var exceptionManager =

  25:          EnterpriseLibraryContainer.Current.

  26:              GetInstance<Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager>();

  27:   

  28:              Assert.IsNotNull(exceptionManager);

  29:          }

其1地点我们读取一个单独的文书EntLib.config.xml,当时也可用别的措施。但这一个格局无法分析自定义的对象。平日大家更愿意一贯自定义使用ServiceLocator,如下:

   1:          [TestMethod]

   2:          public void TestGetExceptionManagerFromCustomerServiceLocator()

   3:          {

   4:              var container = new UnityContainer();

   5:   

   6:              string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EntLib.config.xml");

   7:              var map = new ExeConfigurationFileMap { ExeConfigFilename = path };

   8:   

   9:              var config

  10:                  = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

  11:              var section

  12:                  = (UnityConfigurationSection)config.GetSection("unity");

  13:              section.Configure(container, "DefContainer");

  14:   

  15:              var serviceocator = new UnityServiceLocator(container);

  16:   

  17:              var exceptionManager = serviceocator.

  18:                  GetInstance<Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager>();

  19:   

  20:              Assert.IsNotNull(exceptionManager);

  21:          }

小心,XML必要如此布署,关键在第贰行,必须加个extension才能获取EnterpriseLibrary中的Object

   1:    <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">

   2:      <container name="DefContainer">

   3:        <extension type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension,Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

   4:      </container>

   5:    </unity>

下边大家测试一下自定义的Object,XML配置如下:

   1:    <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">

   2:      <alias alias="IEmployeeBusinessObject" type="BusinessObject.IEmployeeBusinessObject, BusinessObject" />

   3:      <alias alias="EmployeeBusinessObject" type="BusinessObject.EmployeeBusinessObject, BusinessObject" />

   4:      <container name="DefContainer">

   5:        <extension type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension,Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

   6:        <register  type="IEmployeeBusinessObject" mapTo="EmployeeBusinessObject">

   7:          <lifetime type="singleton" />

   8:        </register >

   9:      </container>

  10:    </unity>

UnitTest, IEmployeeBusinessObject是大家定义的二个Interface。

   1:          [TestMethod]

   2:          public void TestDirectGetCustomerobject()

   3:          {

   4:              var container = new UnityContainer();

   5:   

   6:              string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EntLib.config.xml");

   7:              var map = new ExeConfigurationFileMap { ExeConfigFilename = path };

   8:   

   9:              var config

  10:                  = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

  11:              var section

  12:                  = (UnityConfigurationSection)config.GetSection("unity");

  13:              section.Configure(container, "DefContainer");

  14:   

  15:              var serviceocator = new UnityServiceLocator(container);

  16:   

  17:              var employee = serviceocator.GetInstance<IEmployeeBusinessObject>();

  18:   

  19:              Assert.IsNotNull(employee);

  20:          }

 

 

参考:

Creating and Referencing Enterprise Library Objects

 

希望这篇POST对您开发有帮助。

 

作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归作者和新浪共有,欢迎转发,但未经小编同意必须保留此段申明,且在小说页面分明地方给出原作连接,不然保留追究法律权利的权利。
该文章也同时公布在自家的独自博客中-Petter Liu
Blog

相关文章

网站地图xml地图