Skip to content

Commit

Permalink
v1.6.10 - 优化 第一个 Register 设置给 AsyncLocal 默认值;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Nov 30, 2023
1 parent e3edf36 commit 92b75db
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
6 changes: 3 additions & 3 deletions examples/net60_webapi/DB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class DB
var fsql = new FreeSqlCloud("app001");
fsql.DistributeTrace += log => Console.WriteLine(log.Split('\n')[0].Trim());
fsql.Register(DbEnum.db1, () => new FreeSqlBuilder()
fsql.Register(DbEnum.db3, () => new FreeSqlBuilder()
.UseConnectionString(DataType.Sqlite, @"Data Source=:memory:;max pool size=1")
.UseAutoSyncStructure(true)
.Build());
Expand All @@ -30,13 +30,13 @@ public static class DB
.UseAutoSyncStructure(true)
.Build());
fsql.Register(DbEnum.db3, () => new FreeSqlBuilder()
fsql.Register(DbEnum.db1, () => new FreeSqlBuilder()
.UseConnectionString(DataType.Sqlite, @"Data Source=:memory:;max pool size=3")
.UseAutoSyncStructure(true)
.Build());
Console.WriteLine(fsql.Ado.ConnectionString);
using (fsql.Change(DbEnum.db3))
using (fsql.Change(DbEnum.db2))
{
Console.WriteLine(fsql.Ado.ConnectionString);
}
Expand Down
12 changes: 9 additions & 3 deletions src/FreeSql.Cloud/Abstract/FreeSqlCloudBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ public class DBKeyString

public class AsyncLocalAccessor<T>
{
public AsyncLocalAccessor()
Func<T> _defaultValue;
public AsyncLocalAccessor(Func<T> defaultValue)
{
Value = default;
_defaultValue = defaultValue;
}
public T Value
{
get => _asyncLocal.Value != null ? _asyncLocal.Value.Value : default;
get
{
if (_asyncLocal.Value != null) return _asyncLocal.Value.Value;
if (_defaultValue != null) return _defaultValue();
return default;
}
set
{
if (_asyncLocal.Value == null) _asyncLocal.Value = new ValueHolder();
Expand Down
2 changes: 1 addition & 1 deletion src/FreeSql.Cloud/FreeSql.Cloud.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard20;net40</TargetFrameworks>
<Version>1.6.9</Version>
<Version>1.6.10</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
<Description>提供跨数据库访问,分布式事务TCC、SAGA解决方案,支持 .NET Core 2.1+, .NET Framework 4.0+.</Description>
Expand Down
5 changes: 3 additions & 2 deletions src/FreeSql.Cloud/FreeSqlCloud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public TDBKey DBKey

internal TDBKey _dbkeyMaster;

internal AsyncLocalAccessor<TDBKey> _dbkeyCurrent = new AsyncLocalAccessor<TDBKey>();
internal AsyncLocalAccessor<TDBKey> _dbkeyCurrent;
internal TDBKey _dbkey => _dbkeyCurrent.Value;
internal IFreeSql _ormMaster => _ib.Get(_dbkeyMaster);
internal IFreeSql _ormCurrent => _ib.Get(_dbkey);
Expand All @@ -73,7 +73,8 @@ public FreeSqlCloud(string distributeKey)
if (string.IsNullOrWhiteSpace(DistributeKey)) DistributeKey = null;
_ib = new IdleBus<TDBKey, IFreeSql>(TimeSpan.FromMinutes(3));
_ib.Notice += (_, __) => { };
}
_dbkeyCurrent = new AsyncLocalAccessor<TDBKey>(() => _dbkeyMaster);
}

/// <summary>
/// 切换数据库(同一线程,或异步await 后续操作有效)<para></para>
Expand Down
5 changes: 3 additions & 2 deletions src/FreeSql.Cloud/RepositoryCloud/UnitOfWorkManagerCloud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class UnitOfWorkManagerCloud
public UnitOfWorkManagerCloud(FreeSqlCloudBase cloud)
{
Cloud = cloud;
}
_dbkeyCurrent = new AsyncLocalAccessor<string>(Cloud.GetDBKey);
}

public void Dispose()
{
Expand All @@ -25,7 +26,7 @@ protected void ForEachUowManagers(Action<UnitOfWorkManager> action)
foreach (var uowm in _uowManagers.Values) action(uowm);
}

internal AsyncLocalAccessor<string> _dbkeyCurrent = new AsyncLocalAccessor<string>();
internal AsyncLocalAccessor<string> _dbkeyCurrent;
internal string GetDBKey()
{
if (string.IsNullOrWhiteSpace(_dbkeyCurrent.Value) || GetUnitOfWorkManager(_dbkeyCurrent.Value).Current == null) return Cloud.GetDBKey();
Expand Down

0 comments on commit 92b75db

Please sign in to comment.