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>
<returns></returns>
</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)">
<summary>
使用原始连接池ado.net、odbc、oledb<para></para>
@ -5002,6 +5010,11 @@
</summary>
<returns></returns>
</member>
<member name="F:FreeSql.Internal.Utils.ChacheTableEntityFactory">
<summary>
用于解决多实例情况下的静态集合缓存问题
</summary>
</member>
<member name="T:FreeSql.CoreStrings">
<summary>
<para>

View File

@ -11,6 +11,7 @@ using System.Linq.Expressions;
using System.Runtime;
using FreeSql.Internal.Model.Interface;
using System.Threading;
using FreeSql.Internal.Model;
namespace FreeSql
{
@ -50,6 +51,18 @@ namespace FreeSql
_providerType = providerType;
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>
/// 使用原始连接池ado.net、odbc、oledb<para></para>
/// 默认false<para></para>
@ -63,7 +76,7 @@ namespace FreeSql
/// <returns></returns>
public FreeSqlBuilder UseAdoConnectionPool(bool value)
{
_isAdoConnectionPool = value ;
_isAdoConnectionPool = value;
return this;
}
/// <summary>
@ -577,7 +590,8 @@ namespace FreeSql
{
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(
Expression.Constant(typeHandler, typeof(ITypeHandler)),
typeof(ITypeHandler).GetMethod(nameof(typeHandler.Deserialize)),

View File

@ -19,8 +19,22 @@ namespace FreeSql.Internal
{
public class Utils
{
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, TableInfo>> _cacheGetTableByEntity = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, TableInfo>>();
/// <summary>
/// 用于解决多实例情况下的静态集合缓存问题
/// </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)
{
if (entity.IsAnonymousType() ||
@ -534,7 +548,7 @@ namespace FreeSql.Internal
{
col.Attribute.IsNullable = false;
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:
col.Attribute.DbType += " NOT NULL"; //sqlite 主键也可以插入 null