首页 > 代码库 > Microsoft.Practices.Unity 通用类

Microsoft.Practices.Unity 通用类

类库的用法网上有很多,就不多说了,下面稍微封装了一下,记个笔记.

结合泛型接口类型和配置文件,得到IUnityContainer实例存于键值对中.

 1 namespace Containers 2 { 3     public sealed class ObjectContainer 4     { 5         //fields 6         private static readonly object containerLock = new object(); 7         private static Dictionary<string,IUnityContainer> containers= 8             new Dictionary<string, IUnityContainer>(); 9         //*methods10         public static IObject CreateObject<IObject>(){ 11             IUnityContainer container = null;12             lock (containerLock) {13                 Type type = typeof(IObject);14                 string name = type.Assembly.GetName().Name;15                 string configName = (Path.GetDirectoryName(type.Assembly.CodeBase)+16                     @"\"+name+".container").Substring(6);17                 if(containers.ContainsKey(name)){ 18                     container = containers[name];19                 }else{20                     container = LoadContainer(configName);21                     if(container != null){22                         containers.Add(name ,container);23                     }24                 }25             }26             if(container != null){27                 return container.Resolve<IObject>(new ResolverOverride[0]);28             }29             return default(IObject);30         }31 32         public static object CreateObject(Type interfaceType)33         {34             IUnityContainer container = null;35             lock (containerLock) {36                 string name = interfaceType.Assembly.GetName ().Name;37                 string configName = (Path.GetDirectoryName(interfaceType.Assembly.CodeBase)+38                     @"\"+name+".container").Substring(6);39                 if (containers.ContainsKey (name)) {40                     container = containers [name]; 41                 } else {42                     container = LoadContainer (configName);43                     if (container != null) {44                         containers.Add (name , container);45                     }46                 }47             }48             if (container != null) {49                 return container.Resolve (interfaceType, new ResolverOverride[0]);50             }51             return null;52         }53 54         //build with config55         public static IUnityContainer LoadContainer(string containerName)56         {57             IUnityContainer container = new UnityContainer ();58             ConfigurationFileMap map = new ConfigurationFileMap (){ 59                 MachineConfigFilename = containerName60             }; 61             UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager62                 .OpenMappedMachineConfiguration (map).GetSection ("unity");63             if (section != null) {64                 container.LoadConfiguration (section);65                 return container;66             }67             return null;68         }69     } 70 }

测试一下:

 1 namespace TestClass 2 { 3     public class MyClass : EmptyInterface 4     { 5         public string sayHello() 6         { 7             return "Hello everyOne"; 8         } 9     }10 }

接口

1 namespace TestInterface2 {3     public interface EmptyInterface4     {5         string sayHello();6     }7 }

 

配置文件:

 1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3     <configSections> 4         <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/> 5     </configSections> 6     <unity> 7         <typeAliases> 8             <typeAlias alias="singleton" 9                        type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />10             <typeAlias alias="external"11                        type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity" />12             <typeAlias alias="perThread"13                        type="Microsoft.Practices.Unity.PerThreadLifetimeManager, Microsoft.Practices.Unity" /> 14 15 16             <typeAlias alias="MyClass" type="TestClass.MyClass, TestClass" />17             <typeAlias alias="EmptyInterface" type="TestInterface.EmptyInterface, TestInterface" />18         </typeAliases>19         <containers>20             <container>21                 <types>22                     <type type="EmptyInterface" mapTo="MyClass" />23                     <type type="TestInterface.EmptyInterface, TestInterface" mapTo="TestClass.MyClass, TestClass" name="test">24                         <lifetime type="singleton" />25                     </type>26                 </types>27             </container>28         </containers>29     </unity>30 </configuration>


GO:

 1 namespace Test 2 { 3     class MainClass 4     { 5         public static void Main (string[] args) 6         { 7             EmptyInterface iiterface = ObjectContainer.CreateObject<EmptyInterface> ();  8             Console.WriteLine (iiterface.sayHello ()); 9         }10     }11 }

 

作者:zc
出处:http://www.cnblogs.com/jmzs/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

Microsoft.Practices.Unity 通用类