早在Enterprise Library 5.0之前,就已经有了一个Common Service Locator.在这里假设您对Enterprise Library已经有一定了解。
在Enterprise Library 5.0中,我们通常会使用EnterpriseLibraryContainer.Current,实际上它是IServiceLocator接口。例如下面的代码:
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类中:
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。