类库的用法网上有很多,就不多说了,下面稍微封装了一下,记个笔记.
//2015-11-03 环境 .net 4.5 unity 3.x
结合泛型接口类型和配置文件,得到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 //*methods 10 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 config 55 public static IUnityContainer LoadContainer(string containerName) 56 { 57 IUnityContainer container = new UnityContainer (); 58 ConfigurationFileMap map = new ConfigurationFileMap (){ 59 MachineConfigFilename = containerName 60 }; 61 UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager 62 .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 TestInterface 2 { 3 public interface EmptyInterface 4 { 5 string sayHello(); 6 } 7 }
2015-11-03:修正 不同版本的unity写法有差异
配置文件:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/> </configSections> <unity> <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" /> <alias alias="MyClass" type="TestClass.MyClass, TestClass"/> <alias alias="EmptyInterface" type="TestInterface.EmptyInterface, TestInterface"/> <alias alias="CallHandleInterceptionBehavior" type="TestClass.common.CallHandle,TestClass"/> <container> <extension type="Interception"/> <register type="EmptyInterface" mapTo="MyClass"> <interceptor type="InterfaceInterceptor"/> <interceptionBehavior type="CallHandleInterceptionBehavior" /> </register> </container> </unity> </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
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。