using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace SqlSugar { public class InstanceFactory { static Assembly assembly = Assembly.Load(UtilConstants.AssemblyName); static Dictionary typeCache = new Dictionary(); #region Queryable public static ISugarQueryable GetQueryable(ConnectionConfig currentConnectionConfig) { if (currentConnectionConfig.DbType == DbType.SqlServer) { return new SqlServerQueryable(); } else { var className = "Queryable"; className = GetClassName(currentConnectionConfig.DbType.ToString(), className); var result = CreateInstance>(className); return result; } } public static ISugarQueryable GetQueryable(ConnectionConfig currentConnectionConfig) { if (currentConnectionConfig.DbType == DbType.SqlServer) { return new SqlServerQueryable(); } else { var className = "Queryable"; className = GetClassName(currentConnectionConfig.DbType.ToString(), className); var result = CreateInstance>(className); return result; } } public static ISugarQueryable GetQueryable(ConnectionConfig currentConnectionConfig) { if (currentConnectionConfig.DbType == DbType.SqlServer) { return new SqlServerQueryable(); } else { var className = "Queryable"; className = GetClassName(currentConnectionConfig.DbType.ToString(), className); var result = CreateInstance>(className); return result; } } public static ISugarQueryable GetQueryable(ConnectionConfig currentConnectionConfig) { var className = "Queryable"; className = GetClassName(currentConnectionConfig.DbType.ToString(), className); var result = CreateInstance>(className); return result; } public static ISugarQueryable GetQueryable(ConnectionConfig currentConnectionConfig) { var className = "Queryable"; className = GetClassName(currentConnectionConfig.DbType.ToString(), className); var result = CreateInstance>(className); return result; } public static ISugarQueryable GetQueryable(ConnectionConfig currentConnectionConfig) { var className = "Queryable"; className = GetClassName(currentConnectionConfig.DbType.ToString(), className); var result = CreateInstance>(className); return result; } public static ISugarQueryable GetQueryable(ConnectionConfig currentConnectionConfig) { var className = "Queryable"; className = GetClassName(currentConnectionConfig.DbType.ToString(), className); var result = CreateInstance>(className); return result; } public static ISugarQueryable GetQueryable(ConnectionConfig currentConnectionConfig) { var className = "Queryable"; className = GetClassName(currentConnectionConfig.DbType.ToString(), className); var result = CreateInstance>(className); return result; } #region 9-12 public static ISugarQueryable GetQueryable(ConnectionConfig currentConnectionConfig) { var className = "Queryable"; className = GetClassName(currentConnectionConfig.DbType.ToString(), className); var result = CreateInstance>(className); return result; } public static ISugarQueryable GetQueryable(ConnectionConfig currentConnectionConfig) { var className = "Queryable"; className = GetClassName(currentConnectionConfig.DbType.ToString(), className); var result = CreateInstance>(className); return result; } public static ISugarQueryable GetQueryable(ConnectionConfig currentConnectionConfig) { var className = "Queryable"; className = GetClassName(currentConnectionConfig.DbType.ToString(), className); var result = CreateInstance>(className); return result; } public static ISugarQueryable GetQueryable(ConnectionConfig currentConnectionConfig) { var className = "Queryable"; className = GetClassName(currentConnectionConfig.DbType.ToString(), className); var result = CreateInstance>(className); return result; } #endregion #endregion public static QueryBuilder GetQueryBuilder(ConnectionConfig currentConnectionConfig) { if (currentConnectionConfig.DbType == DbType.SqlServer) { return new SqlServerQueryBuilder(); } else { var result = CreateInstance(GetClassName(currentConnectionConfig.DbType.ToString(), nameof(QueryBuilder))); return result; } } public static InsertBuilder GetInsertBuilder(ConnectionConfig currentConnectionConfig) { var result = CreateInstance(GetClassName(currentConnectionConfig.DbType.ToString(), nameof(InsertBuilder))); return result; } public static UpdateBuilder GetUpdateBuilder(ConnectionConfig currentConnectionConfig) { var result = CreateInstance(GetClassName(currentConnectionConfig.DbType.ToString(), nameof(UpdateBuilder))); return result; } public static DeleteBuilder GetDeleteBuilder(ConnectionConfig currentConnectionConfig) { var result = CreateInstance(GetClassName(currentConnectionConfig.DbType.ToString(), nameof(DeleteBuilder))); return result; } public static ILambdaExpressions GetLambdaExpressions(ConnectionConfig currentConnectionConfig) { if (currentConnectionConfig.DbType == DbType.SqlServer) { return new SqlServerExpressionContext(); } else { var result = CreateInstance(GetClassName(currentConnectionConfig.DbType.ToString(), nameof(ExpressionContext))); return result; } } public static ISqlBuilder GetSqlbuilder(ConnectionConfig currentConnectionConfig) { if (currentConnectionConfig.DbType == DbType.SqlServer) { return new SqlServerBuilder(); } else { var result = CreateInstance(GetClassName(currentConnectionConfig.DbType.ToString(), "Builder")); return result; } } public static UpdateableProvider GetUpdateableProvider(ConnectionConfig currentConnectionConfig) where T : class, new() { if (currentConnectionConfig.DbType == DbType.Oracle) { return new OracleUpdateable(); } else { return new UpdateableProvider(); } } public static DeleteableProvider GetDeleteableProvider(ConnectionConfig currentConnectionConfig) where T : class, new() { if (currentConnectionConfig.DbType == DbType.Oracle) { return new OracleDeleteable(); } else { return new DeleteableProvider(); } } public static InsertableProvider GetInsertableProvider(ConnectionConfig currentConnectionConfig) where T : class, new() { if (currentConnectionConfig.DbType == DbType.Oracle) { return new OracleInsertable(); } else { return new InsertableProvider(); } } public static IDbBind GetDbBind(ConnectionConfig currentConnectionConfig) { if (currentConnectionConfig.DbType == DbType.SqlServer) { return new SqlServerDbBind(); } else { var result = CreateInstance(GetClassName(currentConnectionConfig.DbType.ToString(), "DbBind")); return result; } } public static IDbMaintenance GetDbMaintenance(ConnectionConfig currentConnectionConfig) { var result = CreateInstance(GetClassName(currentConnectionConfig.DbType.ToString(), "DbMaintenance")); return result; } public static IDbFirst GetDbFirst(ConnectionConfig currentConnectionConfig) { var result = CreateInstance(GetClassName(currentConnectionConfig.DbType.ToString(), "DbFirst")); return result; } public static ICodeFirst GetCodeFirst(ConnectionConfig currentConnectionConfig) { var result = CreateInstance(GetClassName(currentConnectionConfig.DbType.ToString(), "CodeFirst")); return result; } public static IAdo GetAdo(ConnectionConfig currentConnectionConfig) { if (currentConnectionConfig.DbType == DbType.SqlServer) { return new SqlServerProvider(); } else { var result = CreateInstance(GetClassName(currentConnectionConfig.DbType.ToString(), "Provider")); return result; } } private static string GetClassName(string type, string name) { return UtilConstants.AssemblyName + "." + type + name; } #region CreateInstance private static Restult CreateInstance(string className) { return CreateInstance(className, typeof(T)); } private static Restult CreateInstance(string className) { return CreateInstance(className, typeof(T), typeof(T2)); } private static Restult CreateInstance(string className) { return CreateInstance(className, typeof(T), typeof(T2), typeof(T3)); } private static Restult CreateInstance(string className) { return CreateInstance(className, typeof(T), typeof(T2), typeof(T3), typeof(T4)); } private static Restult CreateInstance(string className) { return CreateInstance(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5)); } private static Restult CreateInstance(string className) { return CreateInstance(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6)); } private static Restult CreateInstance(string className) { return CreateInstance(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7)); } private static Restult CreateInstance(string className) { return CreateInstance(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8)); } #region 9-12 private static Restult CreateInstance(string className) { return CreateInstance(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9)); } private static Restult CreateInstance(string className) { return CreateInstance(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10)); } private static Restult CreateInstance(string className) { return CreateInstance(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11)); } private static Restult CreateInstance(string className) { return CreateInstance(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)); } #endregion private static Restult CreateInstance(string className, params Type[] types) { var cacheKey = className + string.Join(",", types.Select(it => it.FullName)); Type type; if (typeCache.ContainsKey(cacheKey)) { type = typeCache[cacheKey]; } else { lock (typeCache) { type = Type.GetType(className + "`" + types.Length, true).MakeGenericType(types); Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className)); if (!typeCache.ContainsKey(cacheKey)) { typeCache.Add(cacheKey, type); } } } var result = (Restult)Activator.CreateInstance(type, true); return result; } public static T CreateInstance(string className) { Type type; if (typeCache.ContainsKey(className)) { type = typeCache[className]; } else { lock (typeCache) { type = assembly.GetType(className); Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className)); if (!typeCache.ContainsKey(className)) { typeCache.Add(className, type); } } } var result = (T)Activator.CreateInstance(type, true); return result; } #endregion } }