早在Enterprise Library
5.0之前,就曾闹矣一个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:          }

此地方我们读取一个独自的文本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需要这样安排,关键在第3尽,必须加个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地图