AbstractArchitectureComponentInjector.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4. namespace Blue
  5. {
  6. public abstract class AbstractArchitectureComponentInjector : IArchitectureComponentInjector
  7. {
  8. private Type mComponentType;
  9. private bool canInjectTypeListNotInitiated=true;
  10. private List<Type> canInjectTypeList;
  11. private Dictionary<Type, List<InjectInfo>> injectionMap;
  12. protected IArchitecture architectureInstance;
  13. public AbstractArchitectureComponentInjector(Type componentType)
  14. {
  15. SetComponentType(componentType);
  16. }
  17. public void SetArchitecture(IArchitecture architecture)
  18. {
  19. if (architectureInstance == null)
  20. {
  21. architectureInstance = architecture;
  22. }
  23. }
  24. private void SetComponentType(Type componentType)
  25. {
  26. mComponentType = componentType;
  27. }
  28. public virtual void PrepairInjectionData(Type baseType)
  29. {
  30. if (canInjectTypeListNotInitiated)
  31. {
  32. canInjectTypeListNotInitiated = false;
  33. injectionMap = new Dictionary<Type, List<InjectInfo>>();
  34. canInjectTypeList = GetCanInjectTypeList(mComponentType);
  35. }
  36. FilterInjectInfoList(GetInjectInfo(baseType), mComponentType);
  37. }
  38. public virtual void Inject(IArchitecture architecture)
  39. {
  40. if (injectionMap == null)
  41. {
  42. return;
  43. }
  44. SetArchitecture(architecture);
  45. int injectionCount = injectionMap.Count;
  46. List<Type> types = new List<Type>(injectionCount);
  47. types.AddRange(injectionMap.Keys);
  48. if (injectionCount > 0)
  49. {
  50. for (int i = 0; i < injectionCount; i++)
  51. {
  52. Type baseType = types[i];
  53. object injectObject = GetInjectObject(baseType);
  54. if (injectObject == null)
  55. {
  56. continue;
  57. //throw new Exception("Can not find instance of "+baseType.FullName+" ,please make sure you have registed it.");
  58. }
  59. if (!injectObject.GetType().Equals(baseType))
  60. {
  61. continue;//Not the registed Type
  62. }
  63. List<InjectInfo> typeInjectInfoList = injectionMap[baseType];
  64. foreach (InjectInfo injectInfo in typeInjectInfoList)
  65. {
  66. if (injectInfo.InjectScope == InjectScope.Prototype)
  67. {
  68. InjectPrototype(injectObject, injectInfo.InjectField, injectInfo.InjectType);
  69. }
  70. else
  71. {
  72. object injectInstance = GetInjectInstance(injectInfo.InjectType);
  73. InjectSingleton(injectObject, injectInfo.InjectField, injectInstance);
  74. }
  75. }
  76. }
  77. }
  78. }
  79. public void Dispose()
  80. {
  81. if (canInjectTypeList != null)
  82. {
  83. canInjectTypeList.Clear();
  84. canInjectTypeList = null;
  85. }
  86. if (injectionMap != null)
  87. {
  88. injectionMap.Clear();
  89. injectionMap = null;
  90. }
  91. mComponentType = null;
  92. architectureInstance = null;
  93. }
  94. protected virtual object GetInjectObject(Type baseType)
  95. {
  96. return null;
  97. }
  98. protected virtual object GetInjectInstance(Type injectType)
  99. {
  100. if (TypeChecker.Instance.IsService(injectType))
  101. {
  102. return architectureInstance.GetService(injectType);
  103. }
  104. if (TypeChecker.Instance.IsModel(injectType))
  105. {
  106. return architectureInstance.GetModel(injectType);
  107. }
  108. if (TypeChecker.Instance.IsUtility(injectType))
  109. {
  110. return architectureInstance.GetUtility(injectType);
  111. }
  112. throw new Exception(injectType.FullName+" is not a can inject type!");
  113. }
  114. protected Dictionary<Type, List<InjectInfo>> GetInjectionMap()
  115. {
  116. return injectionMap;
  117. }
  118. protected void InjectSingleton(object injectObject, FieldInfo fieldInfo, object injectInstance)
  119. {
  120. fieldInfo.SetValue(injectObject, injectInstance);
  121. }
  122. protected void InjectPrototype(object injectObject, FieldInfo fieldInfo, Type injectType)
  123. {
  124. fieldInfo.SetValue(injectObject, Activator.CreateInstance(injectType));
  125. }
  126. private List<Type> GetCanInjectTypeList(Type baseType)
  127. {
  128. List<Type> canInjectTypeList = new List<Type>();
  129. Type[] interfaceArr = baseType.GetInterfaces();
  130. for (int i = 0; i < interfaceArr.Length; i++)
  131. {
  132. Type interfaceType = interfaceArr[i];
  133. InjectRuleAttribute attribute = null;
  134. if ((attribute = interfaceType.GetCustomAttribute<InjectRuleAttribute>()) != null)
  135. {
  136. canInjectTypeList.AddRange(attribute.GetCanInjectList());
  137. }
  138. }
  139. return canInjectTypeList;
  140. }
  141. private List<InjectInfo> GetInjectInfo(Type tmpType)
  142. {
  143. List<InjectInfo> injectInfoList = new List<InjectInfo>();
  144. FieldInfo[] tmpFieldArr = tmpType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
  145. int length = 0;
  146. if (tmpFieldArr != null)
  147. {
  148. length = tmpFieldArr.Length;
  149. }
  150. for (int i = 0; i < length; i++)
  151. {
  152. FieldInfo tmpField = tmpFieldArr[i];
  153. AutoInjectAttribute autoInjectAttribute = tmpField.GetCustomAttribute<AutoInjectAttribute>();
  154. if (autoInjectAttribute != null)
  155. {
  156. InjectInfo injectInfo = new InjectInfo()
  157. {
  158. BaseType = tmpType,
  159. InjectField = tmpField,
  160. InjectScope = autoInjectAttribute.GetInjectScope()
  161. };
  162. if (autoInjectAttribute.GetInjectType() != null)
  163. {
  164. if (CheckType(tmpField.FieldType, autoInjectAttribute.GetInjectType()))
  165. {
  166. injectInfo.InjectType = autoInjectAttribute.GetInjectType();
  167. }
  168. else
  169. {
  170. throw new Exception("Can not finish the injection of:" + tmpType.FullName + ": property: " + tmpField.Name + ", please check the injectType!");
  171. }
  172. }
  173. else
  174. {
  175. injectInfo.InjectType = tmpField.FieldType;
  176. }
  177. injectInfoList.Add(injectInfo);
  178. }
  179. }
  180. return injectInfoList;
  181. }
  182. //if the injectinfo not match the inject rule,than remove it from the injectInfoList
  183. //for example,you can not inject a IService instance in a IMolde implementation class
  184. private void FilterInjectInfoList(List<InjectInfo> injectInfoList, Type type)
  185. {
  186. int count = injectInfoList.Count;
  187. for (int i = 0; i < count; i++)
  188. {
  189. InjectInfo injectInfo = injectInfoList[i];
  190. if (CheckIfCanInject(canInjectTypeList, injectInfo.InjectType))
  191. {
  192. if (injectionMap.ContainsKey(injectInfo.BaseType))
  193. {
  194. injectionMap[injectInfo.BaseType].Add(injectInfo);
  195. }
  196. else
  197. {
  198. List<InjectInfo> typeInjectInfoList = new List<InjectInfo>();
  199. typeInjectInfoList.Add(injectInfo);
  200. injectionMap.Add(injectInfo.BaseType, typeInjectInfoList);
  201. }
  202. }
  203. }
  204. }
  205. //check if the fieldType and injectType is mismatch;
  206. private bool CheckType(Type fieldType, Type injectType)
  207. {
  208. if (injectType.Equals(fieldType))
  209. {
  210. return true;
  211. }
  212. if (fieldType.IsInterface)
  213. {
  214. return fieldType.IsAssignableFrom(injectType);
  215. }
  216. return injectType.IsSubclassOf(fieldType);
  217. }
  218. private bool CheckIfCanInject(List<Type> canInjectTypeList, Type injectType)
  219. {
  220. bool result = false;
  221. foreach (var canInjectType in canInjectTypeList)
  222. {
  223. if (canInjectType.IsAssignableFrom(injectType))
  224. {
  225. result = true;
  226. break;
  227. }
  228. }
  229. return result;
  230. }
  231. }
  232. }