Skip to content

Commit

Permalink
v1.6.7 增加 using (fsql.Change()) 用完切回来的支持;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Nov 10, 2023
1 parent ee558a3 commit 5e6b615
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ fsql.Change(DbEnum.db2).Select<T>();
fsql.Use(DbEnum.db2).Select<T>();
//单次有效
using (fsql.Change(DbEnum.db2)) {
//todo..
}
//FreeSql.Cloud v1.6.7 一个范围内切换,之后再切换回去
```

自动定向数据库配置:
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.6</Version>
<Version>1.6.7</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
<Description>提供跨数据库访问,分布式事务TCC、SAGA解决方案,支持 .NET Core 2.1+, .NET Framework 4.0+.</Description>
Expand Down
4 changes: 2 additions & 2 deletions src/FreeSql.Cloud/FreeSqlCloud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public IFreeSql Change(TDBKey dbkey)
var oldkey = _dbkeyCurrent.Value;
if (_distributeTraceEnable && object.Equals(dbkey, oldkey) == false) _distributedTraceCall($"数据库切换[Change] {oldkey} -> {dbkey}");
_dbkeyCurrent.Value = dbkey;
return new FreeSqlCloundSnapshot<TDBKey>(this, dbkey);
return new FreeSqlCloundSnapshot<TDBKey>(this, dbkey, oldkey);
}
/// <summary>
/// 临时使用数据库(单次有效)
Expand All @@ -100,7 +100,7 @@ public IFreeSql Use(TDBKey dbkey)
{
var oldkey = _dbkeyCurrent.Value;
if (_distributeTraceEnable && object.Equals(dbkey, oldkey) == false) _distributedTraceCall($"数据库使用[Use] {dbkey}");
return new FreeSqlCloundSnapshot<TDBKey>(this, dbkey);
return new FreeSqlCloundSnapshot<TDBKey>(this, dbkey, default);
}
internal IFreeSql GetBySnapshot(TDBKey dbkey)
{
Expand Down
14 changes: 12 additions & 2 deletions src/FreeSql.Cloud/FreeSqlCloundSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,29 @@ class FreeSqlCloundSnapshot<TDBKey> : IFreeSql
{
readonly FreeSqlCloud<TDBKey> _fsqlc;
readonly TDBKey _current;
readonly TDBKey _old;

public FreeSqlCloundSnapshot(FreeSqlCloud<TDBKey> fsqlc, TDBKey current)
public FreeSqlCloundSnapshot(FreeSqlCloud<TDBKey> fsqlc, TDBKey current, TDBKey old)
{
_fsqlc = fsqlc;
_current = current;
_old = old;
}

public IAdo Ado => _fsqlc.GetBySnapshot(_current).Ado;
public IAop Aop => _fsqlc.GetBySnapshot(_current).Aop;
public ICodeFirst CodeFirst => _fsqlc.GetBySnapshot(_current).CodeFirst;
public IDbFirst DbFirst => _fsqlc.GetBySnapshot(_current).DbFirst;
public GlobalFilter GlobalFilter => _fsqlc.GetBySnapshot(_current).GlobalFilter;
public void Dispose() { }
public void Dispose()
{
//示例
//using (_fsqlc.Change("db2"))
//{

//}
_fsqlc.Change(_old);
}

public void Transaction(Action handler) => _fsqlc.GetBySnapshot(_current).Transaction(handler);
public void Transaction(IsolationLevel isolationLevel, Action handler) => _fsqlc.GetBySnapshot(_current).Transaction(isolationLevel, handler);
Expand Down

0 comments on commit 5e6b615

Please sign in to comment.