update

2881099
2021-11-16 15:45:14 +08:00
parent 4b0b3d21e0
commit 3cf0ff4b98
4 changed files with 3 additions and 79 deletions

1
.gitignore vendored

@@ -3,3 +3,4 @@
################################################################################
/.vs/FreeSql.wiki/v17
/.vs/FreeSql.wiki/project-colors.json

@@ -2,6 +2,6 @@
"ExpandedNodes": [
""
],
"SelectedNode": "\\查询父子关系.md",
"SelectedNode": "\\修改.md",
"PreviewInSolutionExplorer": false
}

Binary file not shown.

@@ -53,7 +53,7 @@ using (TransactionScope ts = new TransactionScope())
分布式数据库 TCC/SAGA 方案请移步https://github.com/2881099/FreeSql.Cloud
## 分库1 利用 IdleBus 重写 IFreeSql
## 分库利用 IdleBus 重写 IFreeSql
在 asp.net core 利用 IdleBus 重写 IFreeSql 支持多库操作
@@ -176,85 +176,8 @@ namespace net50_webapi_idlebus
}
```
---
## 分库2 IdleBus
IFreeSql 对应一个数据库分库是不是要定义N个 IFreeSql分库的租户场景那不要定义10000个
IdleBus 空闲对象管理容器有效组织对象重复利用自动创建、销毁解决【实例】过多且长时间占用的问题。有时候想做一个单例对象重复使用提升性能但是定义多了有的又可能一直空闲着占用资源。专门解决又想重复利用又想少占资源的场景。https://github.com/2881099/IdleBus
> dotnet add package IdleBus
```csharp
static IdleBus<IFreeSql> ib = new IdleBus<IFreeSql>(TimeSpan.FromMinutes(10));
ib.Register("db1", () => new FreeSqlBuilder().UseConnectionString(DataType.MySql, "str1").Build());
ib.Register("db2", () => new FreeSqlBuilder().UseConnectionString(DataType.MySql, "str2").Build());
ib.Register("db3", () => new FreeSqlBuilder().UseConnectionString(DataType.SqlServer, "str3").Build());
//...注册很多个
ib.Get("db1").Select<T>().Limit(10).ToList();
```
IdleBus 也是【单例】设计!主要的两个方法,注册,获取。使用 IdleBus 需要弱化 IFreeSql 的存在,每次使用 ib.Get 获取。
```csharp
public static class IdleBusExtesions
{
static AsyncLocal<string> asyncLocalTenantId = new AsyncLocal<string>();
public static IdleBus<IFreeSql> ChangeTenant(this IdleBus<IFreeSql> ib, string tenantId)
{
asyncLocalTenantId.Value = tenantId;
return ib;
}
public static IFreeSql Get(this IdleBus<IFreeSql> ib) => ib.Get(asyncLocalTenantId.Value ?? "db1");
public static IBaseRepository<T> GetRepository<T>(this IdleBus<IFreeSql> ib) where T : class => ib.Get().GetRepository<T>();
//-------------------------------------------------------
static void test()
{
IdleBus<IFreeSql> ib = null; //单例注入
var fsql = ib.Get(); //获取当前租户对应的 IFreeSql
var fsql00102 = ib.ChangeTenant("00102").Get(); //切换租户,后面的操作都是针对 00102
var songRepository = ib.GetRepository<Song>();
var detailRepository = ib.GetRepository<Detail>();
}
//-------------------------------------------------------
public static IServiceCollection AddIdleBusRepository(this IServiceCollection services, IdleBus<IFreeSql> ib, params Assembly[] assemblies)
{
services.AddSingleton(ib);
services.AddScoped(typeof(IBaseRepository<>), typeof(YourDefaultRepository<>));
services.AddScoped(typeof(BaseRepository<>), typeof(YourDefaultRepository<>));
services.AddScoped(typeof(IBaseRepository<,>), typeof(YourDefaultRepository<,>));
services.AddScoped(typeof(BaseRepository<,>), typeof(YourDefaultRepository<,>));
if (assemblies?.Any() == true)
foreach (var asse in assemblies) //批量注册
foreach (var repo in asse.GetTypes().Where(a => a.IsAbstract == false && typeof(IBaseRepository).IsAssignableFrom(a)))
services.AddScoped(repo);
return services;
}
}
class YourDefaultRepository<T> : BaseRepository<T> where T : class
{
public YourDefaultRepository(IdleBus<IFreeSql> ib) : base(ib.Get(), null, null) { }
}
class YourDefaultRepository<T, TKey> : BaseRepository<T, TKey> where T : class
{
public YourDefaultRepository(IdleBus<IFreeSql> ib) : base(ib.Get(), null, null) { }
}
```
分库总结:
- 跨库 可以使用 ib.Get() 获取 IFreeSql 进行 CRUD
- 跨库 事务不好处理,注意了;
- 跨库 查询不好处理,注意了;