Updated DI UnitOfWorkManager (markdown)

2881099
2025-04-02 09:55:48 +08:00
parent 22181278d1
commit 8d47714ae6

@@ -1,6 +1,6 @@
本篇文章内容引导,如何在 asp.net core 项目中使用特性(注解) 的方式管理事务。
> UnitOfWorkManager 只可以管理 Repository 仓储对象的事务,直接 fsql.Insert\<T>() 是不行的!!但是可以用 repository.Orm.Insert\<T\>repository.Orm 是特殊实现的 IFreeSql与 当前事务保持一致。
> UnitOfWorkManager 只可以管理 Repository 仓储对象的事务
支持六种传播方式(propagation),意味着跨方法的事务非常方便,并且支持同步异步:
@@ -68,9 +68,10 @@ public class TransactionalAttribute : Rougamo.MoAttribute
//Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IFreeSql>(fsql);
services.AddScoped<UnitOfWorkManager>();
services.AddFreeRepository(null, typeof(Startup).Assembly);
services.AddFreeRepository(typeof(Startup).Assembly);
services.AddScoped<IFreeSql>(r => r.GetService<UnitOfWorkManager>().Orm);
services.AddScoped<UnitOfWorkManager>(r => new UnitOfWorkManager(fsql));
//以这种方式注入的 IFreeSql 会跟随 UowManager 切换事务,原始 fsql 对象为 static 单例
//批量注入 Service
}