IocFactory容器实体
Posted 小小的菜鸟程序员
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IocFactory容器实体相关的知识,希望对你有一定的参考价值。
1 /// <summary> 2 /// 此类作用:容器实体,使用App_Data\unity.config文件配置,根据XML配置初始化IOC Created by ZhangQC 2016.08.08 3 /// </summary> 4 public partial class IocFactory 5 { 6 /// <summary> 7 /// 依赖注入容器 8 /// </summary> 9 private static IUnityContainer _container; 10 11 /// <summary> 12 /// 构造函数 13 /// </summary> 14 static IocFactory() 15 { 16 //初始化依赖注入容器 17 InitContainer(); 18 } 19 20 /// <summary> 21 /// 初始化容器 22 /// </summary> 23 private static void InitContainer() 24 { 25 //实例化容器 26 _container = new UnityContainer(); 27 28 _container.AddNewExtension<Interception>(); 29 30 // Unity只会调用标识了InjectionConstructor特性的构造函数,这样就很好的解决了多构造函数的情况下,Unity调用哪个构造函数。 31 32 var unityConfig = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"App_Data\unity.config"); 33 if (!File.Exists(unityConfig)) 34 { 35 return; 36 } 37 38 var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = unityConfig }; 39 var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); 40 var unitySection = (UnityConfigurationSection)configuration.GetSection("unity"); 41 _container.LoadConfiguration(unitySection); 42 } 43 44 /// <summary> 45 /// 容器对象 46 /// </summary> 47 [Obsolete("容器对象,放开之后,可以随便操作,但千万千万不要随便操作,挂了后果自负!")] 48 public static IUnityContainer UnityContainer 49 { 50 get 51 { 52 return _container; 53 } 54 } 55 56 /// <summary> 57 /// 根据类型,返回注册的实例 58 /// </summary> 59 /// <typeparam name="T">各种实例</typeparam> 60 /// <returns>各种实例</returns> 61 [Obsolete("不建议使用,请使用Resolve", true)] 62 public static T GetBizInstance<T>() 63 { 64 return Resolve<T>(); 65 } 66 67 /// <summary> 68 /// 根据类型,返回注册的实例 69 /// </summary> 70 /// <typeparam name="T">各种实例</typeparam> 71 /// <returns>各种实例</returns> 72 public static T Resolve<T>() 73 { 74 return _container.Resolve<T>(); 75 } 76 77 /// <summary> 78 /// 根据类型,返回注册的实例 79 /// </summary> 80 /// <typeparam name="T">各种实例</typeparam> 81 /// <param name="name">注册的别名</param> 82 /// <returns>各种实例</returns> 83 public static T Resolve<T>(string name) 84 { 85 return _container.Resolve<T>(name); 86 } 87 88 /// <summary> 89 /// 返回所有已经注册的实例 90 /// </summary> 91 /// <typeparam name="T">接口</typeparam> 92 /// <param name="resolverOverrides"></param> 93 /// <returns>实体</returns> 94 public IEnumerable<T> ResolveAll<T>(params ResolverOverride[] resolverOverrides) 95 { 96 return _container.ResolveAll<T>(resolverOverrides); 97 } 98 99 /// <summary> 100 /// 判断类型是否已注册 101 /// </summary> 102 /// <typeparam name="T">类型</typeparam> 103 /// <returns>true已注册,false未注册</returns> 104 public static bool IsRegistered<T>() 105 { 106 return _container.IsRegistered<T>(); 107 } 108 109 /// <summary> 110 /// 判断类型是否已注册 111 /// </summary> 112 /// <typeparam name="T">类型</typeparam> 113 /// <returns>true已注册,false未注册</returns> 114 public static bool IsRegistered<T>(string name) 115 { 116 return _container.IsRegistered<T>(name); 117 } 118 119 ///// <summary> 120 ///// 注册指定子类至父类 121 ///// </summary> 122 ///// <typeparam name="TFrom">父类或接口</typeparam> 123 ///// <typeparam name="TTo">子类或实现</typeparam> 124 ///// <returns>各种实例</returns> 125 //public static void RegisterType<TFrom, TTo>() where TTo : TFrom 126 //{ 127 // _container.RegisterType<TFrom, TTo>(); 128 //} 129 /// <summary> 130 /// 注册指定子类至父类 131 /// </summary> 132 /// <typeparam name="TFrom">父类或接口</typeparam> 133 /// <typeparam name="TTo">子类或实现</typeparam> 134 /// <param name="name">注册的别名</param> 135 /// <returns>各种实例</returns> 136 public static void RegisterType<TFrom, TTo>(string name) where TTo : TFrom 137 { 138 _container.RegisterType<TFrom, TTo>(name); 139 } 140 141 /// <summary> 142 /// Register a type mapping with the container. 143 /// 144 /// </summary> 145 /// 146 /// <remarks> 147 /// 148 /// <para> 149 /// This method is used to tell the container that when asked for type <typeparamref name="TFrom"/>, 150 /// actually return an instance of type <typeparamref name="TTo"/>. This is very useful for 151 /// getting instances of interfaces. 152 /// 153 /// </para> 154 /// 155 /// <para> 156 /// This overload registers a default mapping and transient lifetime. 157 /// 158 /// </para> 159 /// 160 /// </remarks> 161 /// <typeparam name="TFrom"><see cref="T:System.Type"/> that will be requested.</typeparam><typeparam name="TTo"><see cref="T:System.Type"/> that will actually be returned.</typeparam><param name="container">Container to configure.</param><param name="injectionMembers">Injection configuration objects.</param> 162 /// <returns> 163 /// The <see cref="T:Microsoft.Practices.Unity.UnityContainer"/> object that this method was called on (this in C#, Me in Visual Basic). 164 /// </returns> 165 public static void RegisterType<TFrom, TTo>(params InjectionMember[] injectionMembers) where TTo : TFrom 166 { 167 _container.RegisterType<TFrom, TTo>(injectionMembers); 168 } 169 170 /// <summary> 171 /// 注册接口的实例 172 /// </summary> 173 /// <param name="name">别名</param> 174 /// <param name="instance">实例,即new XX()</param> 175 /// <typeparam name="TInterface">接口</typeparam> 176 public void RegisterInstance<TInterface>(string name, TInterface instance) 177 { 178 _container.RegisterInstance<TInterface>(name, instance); 179 } 180 181 }
以上是关于IocFactory容器实体的主要内容,如果未能解决你的问题,请参考以下文章
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段