You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
16 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace SqlSugar
  6. {
  7. public class InstanceFactory
  8. {
  9. static Assembly assembly = Assembly.Load(UtilConstants.AssemblyName);
  10. static Dictionary<string, Type> typeCache = new Dictionary<string, Type>();
  11. #region Queryable
  12. public static ISugarQueryable<T> GetQueryable<T>(ConnectionConfig currentConnectionConfig)
  13. {
  14. if (currentConnectionConfig.DbType == DbType.SqlServer)
  15. {
  16. return new SqlServerQueryable<T>();
  17. }
  18. else
  19. {
  20. var className = "Queryable";
  21. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  22. var result = CreateInstance<T, ISugarQueryable<T>>(className);
  23. return result;
  24. }
  25. }
  26. public static ISugarQueryable<T, T2> GetQueryable<T, T2>(ConnectionConfig currentConnectionConfig)
  27. {
  28. if (currentConnectionConfig.DbType == DbType.SqlServer)
  29. {
  30. return new SqlServerQueryable<T, T2>();
  31. }
  32. else
  33. {
  34. var className = "Queryable";
  35. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  36. var result = CreateInstance<T, T2, ISugarQueryable<T, T2>>(className);
  37. return result;
  38. }
  39. }
  40. public static ISugarQueryable<T, T2, T3> GetQueryable<T, T2, T3>(ConnectionConfig currentConnectionConfig)
  41. {
  42. if (currentConnectionConfig.DbType == DbType.SqlServer)
  43. {
  44. return new SqlServerQueryable<T, T2, T3>();
  45. }
  46. else
  47. {
  48. var className = "Queryable";
  49. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  50. var result = CreateInstance<T, T2, T3, ISugarQueryable<T, T2, T3>>(className);
  51. return result;
  52. }
  53. }
  54. public static ISugarQueryable<T, T2, T3, T4> GetQueryable<T, T2, T3, T4>(ConnectionConfig currentConnectionConfig)
  55. {
  56. var className = "Queryable";
  57. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  58. var result = CreateInstance<T, T2, T3, T4, ISugarQueryable<T, T2, T3, T4>>(className);
  59. return result;
  60. }
  61. public static ISugarQueryable<T, T2, T3, T4, T5> GetQueryable<T, T2, T3, T4, T5>(ConnectionConfig currentConnectionConfig)
  62. {
  63. var className = "Queryable";
  64. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  65. var result = CreateInstance<T, T2, T3, T4, T5, ISugarQueryable<T, T2, T3, T4, T5>>(className);
  66. return result;
  67. }
  68. public static ISugarQueryable<T, T2, T3, T4, T5, T6> GetQueryable<T, T2, T3, T4, T5, T6>(ConnectionConfig currentConnectionConfig)
  69. {
  70. var className = "Queryable";
  71. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  72. var result = CreateInstance<T, T2, T3, T4, T5, T6, ISugarQueryable<T, T2, T3, T4, T5, T6>>(className);
  73. return result;
  74. }
  75. public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7> GetQueryable<T, T2, T3, T4, T5, T6, T7>(ConnectionConfig currentConnectionConfig)
  76. {
  77. var className = "Queryable";
  78. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  79. var result = CreateInstance<T, T2, T3, T4, T5, T6, T7, ISugarQueryable<T, T2, T3, T4, T5, T6, T7>>(className);
  80. return result;
  81. }
  82. public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> GetQueryable<T, T2, T3, T4, T5, T6, T7, T8>(ConnectionConfig currentConnectionConfig)
  83. {
  84. var className = "Queryable";
  85. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  86. var result = CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8>>(className);
  87. return result;
  88. }
  89. #region 9-12
  90. public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9> GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9>(ConnectionConfig currentConnectionConfig)
  91. {
  92. var className = "Queryable";
  93. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  94. var result = CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9>>(className);
  95. return result;
  96. }
  97. public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10> GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>(ConnectionConfig currentConnectionConfig)
  98. {
  99. var className = "Queryable";
  100. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  101. var result = CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>>(className);
  102. return result;
  103. }
  104. public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(ConnectionConfig currentConnectionConfig)
  105. {
  106. var className = "Queryable";
  107. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  108. var result = CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>>(className);
  109. return result;
  110. }
  111. public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(ConnectionConfig currentConnectionConfig)
  112. {
  113. var className = "Queryable";
  114. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  115. var result = CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>>(className);
  116. return result;
  117. }
  118. #endregion
  119. #endregion
  120. public static QueryBuilder GetQueryBuilder(ConnectionConfig currentConnectionConfig)
  121. {
  122. if (currentConnectionConfig.DbType == DbType.SqlServer)
  123. {
  124. return new SqlServerQueryBuilder();
  125. }
  126. else
  127. {
  128. var result = CreateInstance<QueryBuilder>(GetClassName(currentConnectionConfig.DbType.ToString(), nameof(QueryBuilder)));
  129. return result;
  130. }
  131. }
  132. public static InsertBuilder GetInsertBuilder(ConnectionConfig currentConnectionConfig)
  133. {
  134. var result = CreateInstance<InsertBuilder>(GetClassName(currentConnectionConfig.DbType.ToString(), nameof(InsertBuilder)));
  135. return result;
  136. }
  137. public static UpdateBuilder GetUpdateBuilder(ConnectionConfig currentConnectionConfig)
  138. {
  139. var result = CreateInstance<UpdateBuilder>(GetClassName(currentConnectionConfig.DbType.ToString(), nameof(UpdateBuilder)));
  140. return result;
  141. }
  142. public static DeleteBuilder GetDeleteBuilder(ConnectionConfig currentConnectionConfig)
  143. {
  144. var result = CreateInstance<DeleteBuilder>(GetClassName(currentConnectionConfig.DbType.ToString(), nameof(DeleteBuilder)));
  145. return result;
  146. }
  147. public static ILambdaExpressions GetLambdaExpressions(ConnectionConfig currentConnectionConfig)
  148. {
  149. if (currentConnectionConfig.DbType == DbType.SqlServer)
  150. {
  151. return new SqlServerExpressionContext();
  152. }
  153. else
  154. {
  155. var result = CreateInstance<ILambdaExpressions>(GetClassName(currentConnectionConfig.DbType.ToString(), nameof(ExpressionContext)));
  156. return result;
  157. }
  158. }
  159. public static ISqlBuilder GetSqlbuilder(ConnectionConfig currentConnectionConfig)
  160. {
  161. if (currentConnectionConfig.DbType == DbType.SqlServer)
  162. {
  163. return new SqlServerBuilder();
  164. }
  165. else
  166. {
  167. var result = CreateInstance<ISqlBuilder>(GetClassName(currentConnectionConfig.DbType.ToString(), "Builder"));
  168. return result;
  169. }
  170. }
  171. public static UpdateableProvider<T> GetUpdateableProvider<T>(ConnectionConfig currentConnectionConfig) where T : class, new()
  172. {
  173. if (currentConnectionConfig.DbType == DbType.Oracle)
  174. {
  175. return new OracleUpdateable<T>();
  176. }
  177. else
  178. {
  179. return new UpdateableProvider<T>();
  180. }
  181. }
  182. public static DeleteableProvider<T> GetDeleteableProvider<T>(ConnectionConfig currentConnectionConfig) where T : class, new()
  183. {
  184. if (currentConnectionConfig.DbType == DbType.Oracle)
  185. {
  186. return new OracleDeleteable<T>();
  187. }
  188. else
  189. {
  190. return new DeleteableProvider<T>();
  191. }
  192. }
  193. public static InsertableProvider<T> GetInsertableProvider<T>(ConnectionConfig currentConnectionConfig) where T : class, new()
  194. {
  195. if (currentConnectionConfig.DbType == DbType.Oracle)
  196. {
  197. return new OracleInsertable<T>();
  198. }
  199. else
  200. {
  201. return new InsertableProvider<T>();
  202. }
  203. }
  204. public static IDbBind GetDbBind(ConnectionConfig currentConnectionConfig)
  205. {
  206. if (currentConnectionConfig.DbType == DbType.SqlServer)
  207. {
  208. return new SqlServerDbBind();
  209. }
  210. else
  211. {
  212. var result = CreateInstance<IDbBind>(GetClassName(currentConnectionConfig.DbType.ToString(), "DbBind"));
  213. return result;
  214. }
  215. }
  216. public static IDbMaintenance GetDbMaintenance(ConnectionConfig currentConnectionConfig)
  217. {
  218. var result = CreateInstance<IDbMaintenance>(GetClassName(currentConnectionConfig.DbType.ToString(), "DbMaintenance"));
  219. return result;
  220. }
  221. public static IDbFirst GetDbFirst(ConnectionConfig currentConnectionConfig)
  222. {
  223. var result = CreateInstance<IDbFirst>(GetClassName(currentConnectionConfig.DbType.ToString(), "DbFirst"));
  224. return result;
  225. }
  226. public static ICodeFirst GetCodeFirst(ConnectionConfig currentConnectionConfig)
  227. {
  228. var result = CreateInstance<ICodeFirst>(GetClassName(currentConnectionConfig.DbType.ToString(), "CodeFirst"));
  229. return result;
  230. }
  231. public static IAdo GetAdo(ConnectionConfig currentConnectionConfig)
  232. {
  233. if (currentConnectionConfig.DbType == DbType.SqlServer)
  234. {
  235. return new SqlServerProvider();
  236. }
  237. else
  238. {
  239. var result = CreateInstance<IAdo>(GetClassName(currentConnectionConfig.DbType.ToString(), "Provider"));
  240. return result;
  241. }
  242. }
  243. private static string GetClassName(string type, string name)
  244. {
  245. return UtilConstants.AssemblyName + "." + type + name;
  246. }
  247. #region CreateInstance
  248. private static Restult CreateInstance<T, Restult>(string className)
  249. {
  250. return CreateInstance<Restult>(className, typeof(T));
  251. }
  252. private static Restult CreateInstance<T, T2, Restult>(string className)
  253. {
  254. return CreateInstance<Restult>(className, typeof(T), typeof(T2));
  255. }
  256. private static Restult CreateInstance<T, T2, T3, Restult>(string className)
  257. {
  258. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3));
  259. }
  260. private static Restult CreateInstance<T, T2, T3, T4, Restult>(string className)
  261. {
  262. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4));
  263. }
  264. private static Restult CreateInstance<T, T2, T3, T4, T5, Restult>(string className)
  265. {
  266. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5));
  267. }
  268. private static Restult CreateInstance<T, T2, T3, T4, T5, T6, Restult>(string className)
  269. {
  270. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6));
  271. }
  272. private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, Restult>(string className)
  273. {
  274. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7));
  275. }
  276. private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, Restult>(string className)
  277. {
  278. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8));
  279. }
  280. #region 9-12
  281. private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, Restult>(string className)
  282. {
  283. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9));
  284. }
  285. private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, Restult>(string className)
  286. {
  287. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10));
  288. }
  289. private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Restult>(string className)
  290. {
  291. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11));
  292. }
  293. private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Restult>(string className)
  294. {
  295. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11), typeof(T12));
  296. }
  297. #endregion
  298. private static Restult CreateInstance<Restult>(string className, params Type[] types)
  299. {
  300. var cacheKey = className + string.Join(",", types.Select(it => it.FullName));
  301. Type type;
  302. if (typeCache.ContainsKey(cacheKey))
  303. {
  304. type = typeCache[cacheKey];
  305. }
  306. else
  307. {
  308. lock (typeCache)
  309. {
  310. type = Type.GetType(className + "`" + types.Length, true).MakeGenericType(types);
  311. Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className));
  312. if (!typeCache.ContainsKey(cacheKey))
  313. {
  314. typeCache.Add(cacheKey, type);
  315. }
  316. }
  317. }
  318. var result = (Restult)Activator.CreateInstance(type, true);
  319. return result;
  320. }
  321. public static T CreateInstance<T>(string className)
  322. {
  323. Type type;
  324. if (typeCache.ContainsKey(className))
  325. {
  326. type = typeCache[className];
  327. }
  328. else
  329. {
  330. lock (typeCache)
  331. {
  332. type = assembly.GetType(className);
  333. Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className));
  334. if (!typeCache.ContainsKey(className))
  335. {
  336. typeCache.Add(className, type);
  337. }
  338. }
  339. }
  340. var result = (T)Activator.CreateInstance(type, true);
  341. return result;
  342. }
  343. #endregion
  344. }
  345. }