luoyunchong / dotnetcore.cap.provider Goto Github PK
View Code? Open in Web Editor NEW为DotNetCore.CAP 增加FreeSql中的统一事务提交,基于如下类库
Home Page: https://github.com/dotnetcore/CAP
为DotNetCore.CAP 增加FreeSql中的统一事务提交,基于如下类库
Home Page: https://github.com/dotnetcore/CAP
大哥好,你现在这样封装肯定可以。
问题初衷:
如何在外部控制事务提交?
因为提交事务的操作中可能还有其他操作,不是简单 tran.Commit() 就完事。
如果把事务提交权交给 Cap,整个事务就终断。
伪代码如下:
using (var uow = ....())
{
_capPublisher.Transaction.Value.Begin(uow.Transaction);
//...
uow.Commit(); // 这样提交 _capPublisher.Flush 没有被执行
}
using (var uow = ....())
{
_capPublisher.Transaction.Value.Begin(uow.Transaction);
//...
capPublisher.Commit(); // 这样提交 uow.Commit 内部其他逻辑没有被执行
}
建议:开放 Flush 方法给外部使用
Cap 作者否决了我的意图,不给支持,其实很简单的一个改动。如果每个数据库自己 copy 代码来改比较复杂,建议代码如下:
public static class CapUnitOfWorkExtenssions
{
public static void SetCapPublisher(this IUnitOfWork fsql, ICapPublisher capPublisher)
{
capPublisher.Transaction.Value.Begin(uow.GetOrBeginTransaction());
}
public static void Flush(this ICapPublisher capPublisher)
{
capPublisher?.GetType().GetMethod("Flush", BindingFlags.Instance | BindingFlags.NonPublic)
.Invoke(capPublisher, null);
}
}
using (var uow = fsql.CreateUnitOfWork())
{
uow.SetCapPublisher(_capPublisher);
//...
uow.Commit();
capPublisher.Flush(); //执行 mq 推送
}
这样一来就不用为每个数据库去做这个了
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.