Browse Source

-

pull/12/MERGE
sunkaixuan 8 years ago
parent
commit
ff794fbebb
  1. 257
      SqlSugar/InstanceFactory.cs

257
SqlSugar/InstanceFactory.cs

@ -16,121 +16,121 @@ namespace SqlSugar
{ {
string className = "Queryable"; string className = "Queryable";
className = GetClassName(currentConnectionConfig.DbType, className); className = GetClassName(currentConnectionConfig.DbType, className);
ISugarQueryable<T> reval = CreateInstance<T, ISugarQueryable<T>>(className, currentConnectionConfig.DbType); ISugarQueryable<T> result = CreateInstance<T, ISugarQueryable<T>>(className);
return reval; return result;
} }
public static ISugarQueryable<T, T2> GetQueryable<T, T2>(ConnectionConfig currentConnectionConfig) public static ISugarQueryable<T, T2> GetQueryable<T, T2>(ConnectionConfig currentConnectionConfig)
{ {
string className = "Queryable"; string className = "Queryable";
className = GetClassName(currentConnectionConfig.DbType, className); className = GetClassName(currentConnectionConfig.DbType, className);
ISugarQueryable<T, T2> reval = CreateInstance<T, T2, ISugarQueryable<T, T2>>(className, currentConnectionConfig.DbType); ISugarQueryable<T, T2> result = CreateInstance<T, T2, ISugarQueryable<T, T2>>(className);
return reval; return result;
} }
public static ISugarQueryable<T, T2, T3> GetQueryable<T, T2, T3>(ConnectionConfig currentConnectionConfig) public static ISugarQueryable<T, T2, T3> GetQueryable<T, T2, T3>(ConnectionConfig currentConnectionConfig)
{ {
string className = "Queryable"; string className = "Queryable";
className = GetClassName(currentConnectionConfig.DbType, className); className = GetClassName(currentConnectionConfig.DbType, className);
ISugarQueryable<T, T2, T3> reval = CreateInstance<T, T2, T3, ISugarQueryable<T, T2, T3>>(className, currentConnectionConfig.DbType); ISugarQueryable<T, T2, T3> result = CreateInstance<T, T2, T3, ISugarQueryable<T, T2, T3>>(className);
return reval; return result;
} }
public static ISugarQueryable<T, T2, T3, T4> GetQueryable<T, T2, T3, T4>(ConnectionConfig currentConnectionConfig) public static ISugarQueryable<T, T2, T3, T4> GetQueryable<T, T2, T3, T4>(ConnectionConfig currentConnectionConfig)
{ {
string className = "Queryable"; string className = "Queryable";
className = GetClassName(currentConnectionConfig.DbType, className); className = GetClassName(currentConnectionConfig.DbType, className);
ISugarQueryable<T, T2, T3, T4> reval = CreateInstance<T, T2, T3, T4, ISugarQueryable<T, T2, T3, T4>>(className, currentConnectionConfig.DbType); ISugarQueryable<T, T2, T3, T4> result = CreateInstance<T, T2, T3, T4, ISugarQueryable<T, T2, T3, T4>>(className);
return reval; return result;
} }
public static ISugarQueryable<T, T2, T3, T4, T5> GetQueryable<T, T2, T3, T4, T5>(ConnectionConfig currentConnectionConfig) public static ISugarQueryable<T, T2, T3, T4, T5> GetQueryable<T, T2, T3, T4, T5>(ConnectionConfig currentConnectionConfig)
{ {
string className = "Queryable"; string className = "Queryable";
className = GetClassName(currentConnectionConfig.DbType, className); className = GetClassName(currentConnectionConfig.DbType, className);
ISugarQueryable<T, T2, T3, T4, T5> reval = CreateInstance<T, T2, T3, T4, T5, ISugarQueryable<T, T2, T3, T4, T5>>(className, currentConnectionConfig.DbType); ISugarQueryable<T, T2, T3, T4, T5> result = CreateInstance<T, T2, T3, T4, T5, ISugarQueryable<T, T2, T3, T4, T5>>(className);
return reval; return result;
} }
public static ISugarQueryable<T, T2, T3, T4, T5, T6> GetQueryable<T, T2, T3, T4, T5, T6>(ConnectionConfig currentConnectionConfig) public static ISugarQueryable<T, T2, T3, T4, T5, T6> GetQueryable<T, T2, T3, T4, T5, T6>(ConnectionConfig currentConnectionConfig)
{ {
string className = "Queryable"; string className = "Queryable";
className = GetClassName(currentConnectionConfig.DbType, className); className = GetClassName(currentConnectionConfig.DbType, className);
ISugarQueryable<T, T2, T3, T4, T5, T6> reval = CreateInstance<T, T2, T3, T4, T5, T6, ISugarQueryable<T, T2, T3, T4, T5, T6>>(className, currentConnectionConfig.DbType); ISugarQueryable<T, T2, T3, T4, T5, T6> result = CreateInstance<T, T2, T3, T4, T5, T6, ISugarQueryable<T, T2, T3, T4, T5, T6>>(className);
return reval; return result;
} }
public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7> GetQueryable<T, T2, T3, T4, T5, T6, T7>(ConnectionConfig currentConnectionConfig) public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7> GetQueryable<T, T2, T3, T4, T5, T6, T7>(ConnectionConfig currentConnectionConfig)
{ {
string className = "Queryable"; string className = "Queryable";
className = GetClassName(currentConnectionConfig.DbType, className); className = GetClassName(currentConnectionConfig.DbType, className);
ISugarQueryable<T, T2, T3, T4, T5, T6, T7> reval = CreateInstance<T, T2, T3, T4, T5, T6, T7, ISugarQueryable<T, T2, T3, T4, T5, T6, T7>>(className, currentConnectionConfig.DbType); ISugarQueryable<T, T2, T3, T4, T5, T6, T7> result = CreateInstance<T, T2, T3, T4, T5, T6, T7, ISugarQueryable<T, T2, T3, T4, T5, T6, T7>>(className);
return reval; return result;
} }
public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> GetQueryable<T, T2, T3, T4, T5, T6, T7, T8>(ConnectionConfig currentConnectionConfig) public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> GetQueryable<T, T2, T3, T4, T5, T6, T7, T8>(ConnectionConfig currentConnectionConfig)
{ {
string className = "Queryable"; string className = "Queryable";
className = GetClassName(currentConnectionConfig.DbType, className); className = GetClassName(currentConnectionConfig.DbType, className);
ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> reval = CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8>>(className, currentConnectionConfig.DbType); ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> result = CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8>>(className);
return reval; return result;
} }
#endregion #endregion
public static QueryBuilder GetQueryBuilder(ConnectionConfig currentConnectionConfig) public static QueryBuilder GetQueryBuilder(ConnectionConfig currentConnectionConfig)
{ {
QueryBuilder reval = CreateInstance<QueryBuilder>(GetClassName(currentConnectionConfig.DbType, "QueryBuilder"), currentConnectionConfig.DbType); QueryBuilder result = CreateInstance<QueryBuilder>(GetClassName(currentConnectionConfig.DbType, "QueryBuilder"));
return reval; return result;
} }
public static InsertBuilder GetInsertBuilder(ConnectionConfig currentConnectionConfig) public static InsertBuilder GetInsertBuilder(ConnectionConfig currentConnectionConfig)
{ {
InsertBuilder reval = CreateInstance<InsertBuilder>(GetClassName(currentConnectionConfig.DbType, "InsertBuilder"), currentConnectionConfig.DbType); InsertBuilder result = CreateInstance<InsertBuilder>(GetClassName(currentConnectionConfig.DbType, "InsertBuilder"));
return reval; return result;
} }
public static UpdateBuilder GetUpdateBuilder(ConnectionConfig currentConnectionConfig) public static UpdateBuilder GetUpdateBuilder(ConnectionConfig currentConnectionConfig)
{ {
UpdateBuilder reval = CreateInstance<UpdateBuilder>(GetClassName(currentConnectionConfig.DbType, "UpdateBuilder"), currentConnectionConfig.DbType); UpdateBuilder result = CreateInstance<UpdateBuilder>(GetClassName(currentConnectionConfig.DbType, "UpdateBuilder"));
return reval; return result;
} }
public static DeleteBuilder GetDeleteBuilder(ConnectionConfig currentConnectionConfig) public static DeleteBuilder GetDeleteBuilder(ConnectionConfig currentConnectionConfig)
{ {
DeleteBuilder reval = CreateInstance<DeleteBuilder>(GetClassName(currentConnectionConfig.DbType, "DeleteBuilder"), currentConnectionConfig.DbType); DeleteBuilder result = CreateInstance<DeleteBuilder>(GetClassName(currentConnectionConfig.DbType, "DeleteBuilder"));
return reval; return result;
} }
public static ILambdaExpressions GetLambdaExpressions(ConnectionConfig currentConnectionConfig) public static ILambdaExpressions GetLambdaExpressions(ConnectionConfig currentConnectionConfig)
{ {
ILambdaExpressions reval = CreateInstance<ILambdaExpressions>(GetClassName(currentConnectionConfig.DbType, "ExpressionContext"), currentConnectionConfig.DbType); ILambdaExpressions result = CreateInstance<ILambdaExpressions>(GetClassName(currentConnectionConfig.DbType, "ExpressionContext"));
return reval; return result;
} }
public static ISqlBuilder GetSqlbuilder(ConnectionConfig currentConnectionConfig) public static ISqlBuilder GetSqlbuilder(ConnectionConfig currentConnectionConfig)
{ {
ISqlBuilder reval = CreateInstance<ISqlBuilder>(GetClassName(currentConnectionConfig.DbType, "Builder"), currentConnectionConfig.DbType); ISqlBuilder result = CreateInstance<ISqlBuilder>(GetClassName(currentConnectionConfig.DbType, "Builder"));
return reval; return result;
} }
public static IDbBind GetDbBind(ConnectionConfig currentConnectionConfig) public static IDbBind GetDbBind(ConnectionConfig currentConnectionConfig)
{ {
IDbBind reval = CreateInstance<IDbBind>(GetClassName(currentConnectionConfig.DbType, "DbBind"), currentConnectionConfig.DbType); IDbBind result = CreateInstance<IDbBind>(GetClassName(currentConnectionConfig.DbType, "DbBind"));
return reval; return result;
} }
public static IDbMaintenance GetDbMaintenance(ConnectionConfig currentConnectionConfig) public static IDbMaintenance GetDbMaintenance(ConnectionConfig currentConnectionConfig)
{ {
IDbMaintenance reval = CreateInstance<IDbMaintenance>(GetClassName(currentConnectionConfig.DbType, "DbMaintenance"), currentConnectionConfig.DbType); IDbMaintenance result = CreateInstance<IDbMaintenance>(GetClassName(currentConnectionConfig.DbType, "DbMaintenance"));
return reval; return result;
} }
public static IDbFirst GetDbFirst(ConnectionConfig currentConnectionConfig) public static IDbFirst GetDbFirst(ConnectionConfig currentConnectionConfig)
{ {
IDbFirst reval = CreateInstance<IDbFirst>(GetClassName(currentConnectionConfig.DbType, "DbFirst"), currentConnectionConfig.DbType); IDbFirst result = CreateInstance<IDbFirst>(GetClassName(currentConnectionConfig.DbType, "DbFirst"));
return reval; return result;
} }
public static ICodeFirst GetCodeFirst(ConnectionConfig currentConnectionConfig) public static ICodeFirst GetCodeFirst(ConnectionConfig currentConnectionConfig)
{ {
ICodeFirst reval = CreateInstance<ICodeFirst>(GetClassName(currentConnectionConfig.DbType, "CodeFirst"), currentConnectionConfig.DbType); ICodeFirst result = CreateInstance<ICodeFirst>(GetClassName(currentConnectionConfig.DbType, "CodeFirst"));
return reval; return result;
} }
public static IAdo GetAdo(ConnectionConfig currentConnectionConfig) public static IAdo GetAdo(ConnectionConfig currentConnectionConfig)
{ {
IAdo reval = CreateInstance<IAdo>(GetClassName(currentConnectionConfig.DbType, "Provider"), currentConnectionConfig.DbType); IAdo result = CreateInstance<IAdo>(GetClassName(currentConnectionConfig.DbType, "Provider"));
return reval; return result;
} }
private static string GetClassName(string type, string name) private static string GetClassName(string type, string name)
@ -139,170 +139,41 @@ namespace SqlSugar
} }
#region CreateInstance #region CreateInstance
private static Restult CreateInstance<T, Restult>(string className, string dbType) private static Restult CreateInstance<T, Restult>(string className)
{ {
var cacheKey = className + typeof(T).FullName; return CreateInstance<Restult>(className, typeof(T));
Type type;
if (typeCache.ContainsKey(cacheKey))
{
type = typeCache[cacheKey];
}
else
{
lock (typeCache)
{
type = Type.GetType(className + "`1", true).MakeGenericType(typeof(T));
Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className));
if (!typeCache.ContainsKey(cacheKey))
{
typeCache.Add(cacheKey, type);
}
}
}
var reval = (Restult)Activator.CreateInstance(type, true);
return reval;
} }
private static Restult CreateInstance<T, T2, Restult>(string className, string dbType) private static Restult CreateInstance<T, T2, Restult>(string className)
{ {
var cacheKey = className + typeof(T).FullName + typeof(T2).FullName; return CreateInstance<Restult>(className, typeof(T), typeof(T2));
Type type;
if (typeCache.ContainsKey(cacheKey))
{
type = typeCache[cacheKey];
}
else
{
lock (typeCache)
{
type = Type.GetType(className + "`2", true).MakeGenericType(typeof(T), typeof(T2));
Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className));
if (!typeCache.ContainsKey(cacheKey))
{
typeCache.Add(cacheKey, type);
}
}
}
var reval = (Restult)Activator.CreateInstance(type, true);
return reval;
} }
private static Restult CreateInstance<T, T2, T3, Restult>(string className, string dbType) private static Restult CreateInstance<T, T2, T3, Restult>(string className)
{ {
var cacheKey = className + typeof(T).FullName + typeof(T2).FullName + typeof(T3).FullName; return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3));
Type type;
if (typeCache.ContainsKey(cacheKey))
{
type = typeCache[cacheKey];
}
else
{
lock (typeCache)
{
type = Type.GetType(className + "`3", true).MakeGenericType(typeof(T), typeof(T2), typeof(T3));
Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className));
if (!typeCache.ContainsKey(cacheKey))
{
typeCache.Add(cacheKey, type);
}
}
}
var reval = (Restult)Activator.CreateInstance(type, true);
return reval;
} }
private static Restult CreateInstance<T, T2, T3, T4, Restult>(string className, string dbType) private static Restult CreateInstance<T, T2, T3, T4, Restult>(string className)
{ {
var cacheKey = className + typeof(T).FullName + typeof(T2).FullName + typeof(T3).FullName + typeof(T4).FullName; return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4));
Type type;
if (typeCache.ContainsKey(cacheKey))
{
type = typeCache[cacheKey];
}
else
{
lock (typeCache)
{
type = Type.GetType(className + "`4", true).MakeGenericType(typeof(T), typeof(T2), typeof(T3), typeof(T4));
Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className));
if (!typeCache.ContainsKey(cacheKey))
{
typeCache.Add(cacheKey, type);
}
}
}
var reval = (Restult)Activator.CreateInstance(type, true);
return reval;
} }
private static Restult CreateInstance<T, T2, T3, T4, T5, Restult>(string className, string dbType) private static Restult CreateInstance<T, T2, T3, T4, T5, Restult>(string className)
{ {
var cacheKey = className + typeof(T).FullName + typeof(T2).FullName + typeof(T3).FullName + typeof(T4).FullName + typeof(T5).FullName; return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5));
Type type;
if (typeCache.ContainsKey(cacheKey))
{
type = typeCache[cacheKey];
}
else
{
lock (typeCache)
{
type = Type.GetType(className + "`5", true).MakeGenericType(typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5));
Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className));
if (!typeCache.ContainsKey(cacheKey))
{
typeCache.Add(cacheKey, type);
}
}
}
var reval = (Restult)Activator.CreateInstance(type, true);
return reval;
} }
private static Restult CreateInstance<T, T2, T3, T4, T5, T6, Restult>(string className, string dbType) private static Restult CreateInstance<T, T2, T3, T4, T5, T6, Restult>(string className)
{ {
var cacheKey = className + typeof(T).FullName + typeof(T2).FullName + typeof(T3).FullName + typeof(T4).FullName + typeof(T5).FullName + typeof(T6).FullName; return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5),typeof(T6));
Type type;
if (typeCache.ContainsKey(cacheKey))
{
type = typeCache[cacheKey];
}
else
{
lock (typeCache)
{
type = Type.GetType(className + "`6", true).MakeGenericType(typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6));
Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className));
if (!typeCache.ContainsKey(cacheKey))
{
typeCache.Add(cacheKey, type);
}
}
}
var reval = (Restult)Activator.CreateInstance(type, true);
return reval;
} }
private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, Restult>(string className, string dbType) private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, Restult>(string className)
{ {
var cacheKey = className + typeof(T).FullName + typeof(T2).FullName + typeof(T3).FullName + typeof(T4).FullName + typeof(T5).FullName + typeof(T6).FullName + typeof(T7).FullName; return CreateInstance< Restult > (className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5),typeof(T6),typeof(T7));
Type type; }
if (typeCache.ContainsKey(cacheKey)) private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, Restult>(string className)
{ {
type = typeCache[cacheKey]; return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7),typeof(T8));
}
else
{
lock (typeCache)
{
type = Type.GetType(className + "`7", true).MakeGenericType(typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7));
Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className));
if (!typeCache.ContainsKey(cacheKey))
{
typeCache.Add(cacheKey, type);
}
}
}
var reval = (Restult)Activator.CreateInstance(type, true);
return reval;
} }
private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, Restult>(string className, string dbType) private static Restult CreateInstance<Restult>(string className, params Type[] types)
{ {
var cacheKey = className + typeof(T).FullName + typeof(T2).FullName + typeof(T3).FullName + typeof(T4).FullName + typeof(T5).FullName + typeof(T6).FullName + typeof(T7).FullName + typeof(T8).FullName; var cacheKey = className + string.Join(",", types.Select(it => it.FullName));
Type type; Type type;
if (typeCache.ContainsKey(cacheKey)) if (typeCache.ContainsKey(cacheKey))
{ {
@ -312,7 +183,7 @@ namespace SqlSugar
{ {
lock (typeCache) lock (typeCache)
{ {
type = Type.GetType(className + "`8", true).MakeGenericType(typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8)); type = Type.GetType(className + "`" + types.Length, true).MakeGenericType(types);
Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className)); Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className));
if (!typeCache.ContainsKey(cacheKey)) if (!typeCache.ContainsKey(cacheKey))
{ {
@ -320,10 +191,10 @@ namespace SqlSugar
} }
} }
} }
var reval = (Restult)Activator.CreateInstance(type, true); var result = (Restult)Activator.CreateInstance(type, true);
return reval; return result;
} }
private static T CreateInstance<T>(string className, string dbType) private static T CreateInstance<T>(string className)
{ {
Type type; Type type;
if (typeCache.ContainsKey(className)) if (typeCache.ContainsKey(className))
@ -342,8 +213,8 @@ namespace SqlSugar
} }
} }
} }
var reval = (T)Activator.CreateInstance(type, true); var result = (T)Activator.CreateInstance(type, true);
return reval; return result;
} }
#endregion #endregion
} }

Loading…
Cancel
Save