Merge pull request #1627 from hyzx86/AddTableEntityFactory

Add TableEntityCacheFactory
This commit is contained in:
2881099 2023-09-27 21:52:56 +08:00 committed by GitHub
commit 66f7c659ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 15 deletions

View File

@ -1445,6 +1445,14 @@
<param name="providerType">提供者的类型,一般不需要指定,如果一直提示“缺少 FreeSql 数据库实现包FreeSql.Provider.MySql.dll可前往 nuget 下载”的错误,说明反射获取不到类型,此时该参数可排上用场<para></para>例如typeof(FreeSql.SqlServer.SqlServerProvider&lt;&gt;)</param> <param name="providerType">提供者的类型,一般不需要指定,如果一直提示“缺少 FreeSql 数据库实现包FreeSql.Provider.MySql.dll可前往 nuget 下载”的错误,说明反射获取不到类型,此时该参数可排上用场<para></para>例如typeof(FreeSql.SqlServer.SqlServerProvider&lt;&gt;)</param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:FreeSql.FreeSqlBuilder.UseCustomTableEntityCacheFactory(System.Func{System.Collections.Concurrent.ConcurrentDictionary{FreeSql.DataType,System.Collections.Concurrent.ConcurrentDictionary{System.Type,FreeSql.Internal.Model.TableInfo}}})">
<summary>
用于指定自定义实现TableEntiy 的缓存集合
解决多实例下相同类型映射到不同表的问题
</summary>
<param name="factory"></param>
<returns></returns>
</member>
<member name="M:FreeSql.FreeSqlBuilder.UseAdoConnectionPool(System.Boolean)"> <member name="M:FreeSql.FreeSqlBuilder.UseAdoConnectionPool(System.Boolean)">
<summary> <summary>
使用原始连接池ado.net、odbc、oledb<para></para> 使用原始连接池ado.net、odbc、oledb<para></para>
@ -5002,6 +5010,11 @@
</summary> </summary>
<returns></returns> <returns></returns>
</member> </member>
<member name="F:FreeSql.Internal.Utils.ChacheTableEntityFactory">
<summary>
用于解决多实例情况下的静态集合缓存问题
</summary>
</member>
<member name="T:FreeSql.CoreStrings"> <member name="T:FreeSql.CoreStrings">
<summary> <summary>
<para> <para>

View File

@ -11,6 +11,7 @@ using System.Linq.Expressions;
using System.Runtime; using System.Runtime;
using FreeSql.Internal.Model.Interface; using FreeSql.Internal.Model.Interface;
using System.Threading; using System.Threading;
using FreeSql.Internal.Model;
namespace FreeSql namespace FreeSql
{ {
@ -50,6 +51,18 @@ namespace FreeSql
_providerType = providerType; _providerType = providerType;
return this; return this;
} }
/// <summary>
/// 用于指定自定义实现TableEntiy 的缓存集合
/// 解决多实例下相同类型映射到不同表的问题
/// </summary>
/// <param name="factory"></param>
/// <returns></returns>
public FreeSqlBuilder UseCustomTableEntityCacheFactory(Func<ConcurrentDictionary<DataType, ConcurrentDictionary<Type, TableInfo>>> factory)
{
Utils.ChacheTableEntityFactory = factory;
return this;
}
/// <summary> /// <summary>
/// 使用原始连接池ado.net、odbc、oledb<para></para> /// 使用原始连接池ado.net、odbc、oledb<para></para>
/// 默认false<para></para> /// 默认false<para></para>
@ -63,7 +76,7 @@ namespace FreeSql
/// <returns></returns> /// <returns></returns>
public FreeSqlBuilder UseAdoConnectionPool(bool value) public FreeSqlBuilder UseAdoConnectionPool(bool value)
{ {
_isAdoConnectionPool = value ; _isAdoConnectionPool = value;
return this; return this;
} }
/// <summary> /// <summary>
@ -577,7 +590,8 @@ namespace FreeSql
{ {
FreeSql.Internal.Utils.GetDataReaderValueBlockExpressionSwitchTypeFullName.Add((LabelTarget returnTarget, Expression valueExp, Type type2) => FreeSql.Internal.Utils.GetDataReaderValueBlockExpressionSwitchTypeFullName.Add((LabelTarget returnTarget, Expression valueExp, Type type2) =>
{ {
if (FreeSql.Internal.Utils.TypeHandlers.TryGetValue(type2, out var typeHandler)) { if (FreeSql.Internal.Utils.TypeHandlers.TryGetValue(type2, out var typeHandler))
{
var valueExpRet = Expression.Call( var valueExpRet = Expression.Call(
Expression.Constant(typeHandler, typeof(ITypeHandler)), Expression.Constant(typeHandler, typeof(ITypeHandler)),
typeof(ITypeHandler).GetMethod(nameof(typeHandler.Deserialize)), typeof(ITypeHandler).GetMethod(nameof(typeHandler.Deserialize)),

View File

@ -19,8 +19,22 @@ namespace FreeSql.Internal
{ {
public class Utils public class Utils
{ {
/// <summary>
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, TableInfo>> _cacheGetTableByEntity = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, TableInfo>>(); /// 用于解决多实例情况下的静态集合缓存问题
/// </summary>
public static Func<ConcurrentDictionary<DataType, ConcurrentDictionary<Type, TableInfo>>> ChacheTableEntityFactory = null;
private static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, TableInfo>> __cacheGetTableByEntity = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, TableInfo>>();
public static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, TableInfo>> _cacheGetTableByEntity
{
get
{
if (ChacheTableEntityFactory != null)
{
return ChacheTableEntityFactory.Invoke();
}
return __cacheGetTableByEntity;
}
}
internal static void RemoveTableByEntity(Type entity, CommonUtils common) internal static void RemoveTableByEntity(Type entity, CommonUtils common)
{ {
if (entity.IsAnonymousType() || if (entity.IsAnonymousType() ||
@ -534,7 +548,7 @@ namespace FreeSql.Internal
{ {
col.Attribute.IsNullable = false; col.Attribute.IsNullable = false;
col.Attribute.DbType = col.Attribute.DbType.Replace("NOT NULL", "").Replace(" NULL", "").Trim(); col.Attribute.DbType = col.Attribute.DbType.Replace("NOT NULL", "").Replace(" NULL", "").Trim();
switch(common._orm.Ado.DataType) switch (common._orm.Ado.DataType)
{ {
case DataType.Sqlite: case DataType.Sqlite:
col.Attribute.DbType += " NOT NULL"; //sqlite 主键也可以插入 null col.Attribute.DbType += " NOT NULL"; //sqlite 主键也可以插入 null
@ -1780,16 +1794,16 @@ namespace FreeSql.Internal
Expression.IfThenElse(Expression.Equal(read2ExpValue, Expression.Constant(null)), Expression.IfThenElse(Expression.Equal(read2ExpValue, Expression.Constant(null)),
Expression.Assign(Expression.MakeMemberAccess(ret2Exp, field), Expression.Default(field.FieldType)), Expression.Assign(Expression.MakeMemberAccess(ret2Exp, field), Expression.Default(field.FieldType)),
Expression.Assign(Expression.MakeMemberAccess(ret2Exp, field), Expression.Convert(read2ExpValue, field.FieldType))) Expression.Assign(Expression.MakeMemberAccess(ret2Exp, field), Expression.Convert(read2ExpValue, field.FieldType)))
//), //),
//Expression.Catch(typeof(Exception), Expression.Block( //Expression.Catch(typeof(Exception), Expression.Block(
// Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(0)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 0)))), // Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(0)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 0)))),
// Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(1)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 1)))), // Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(1)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 1)))),
// Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(2)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 2)))), // Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(2)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 2)))),
// Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(3)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 3)))), // Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(3)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 3)))),
// Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(4)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 4)))) // Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(4)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 4))))
// ) // )
//)) //))
}); });
} }
block2Exp.AddRange(new Expression[] { block2Exp.AddRange(new Expression[] {
Expression.Return(returnTarget, Expression.New(RowInfo.Constructor, Expression.Convert(ret2Exp, typeof(object)), dataIndexExp)), Expression.Return(returnTarget, Expression.New(RowInfo.Constructor, Expression.Convert(ret2Exp, typeof(object)), dataIndexExp)),