C# AOP 事务方面
我的问题与收到错误时所做的动作的撤消有关。
在我在.NET Core 5上开发的项目中,我可以使用AOP撤消这些交易,并使用AOP进行横切范围方面。但是我需要一个新的解决方案。让我们假设在交易中将5个CRUD交易发送到DB。 如果这5个CRUD操作中的一个失败。我希望我发送的3笔交易被逆转,其中2笔交易不颠倒。 我该怎么做。
感谢您的帮助。
public class TransactionScopeAspect : MethodInterception
{
public override void Intercept(IInvocation invocation)
{
using (TransactionScope transactionScope = new TransactionScope())
{
try
{
invocation.Proceed();
transactionScope.Complete();
}
catch (System.Exception e)
{
transactionScope.Dispose();
throw;
}
}
}
}
我正在等待通用的transactionscopectect
My problem is related to the undoing of the actions I have done when the error is received.
In the project that I am developing on .Net core 5, I can undo these transactions with Transection scope aspect using AOP. But I need a new solution. Let's assume that 5 crud transactions are sent to the db in the transaction.
If one of these 5 crud operations fails. I want the 3 transactions I sent to be reversed and 2 of them not to be reversed.
how do i do this.
thank you for your help.
public class TransactionScopeAspect : MethodInterception
{
public override void Intercept(IInvocation invocation)
{
using (TransactionScope transactionScope = new TransactionScope())
{
try
{
invocation.Proceed();
transactionScope.Complete();
}
catch (System.Exception e)
{
transactionScope.Dispose();
throw;
}
}
}
}
I'm waiting generic TransactionScopeAspect
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果5个CRUD操作属于同一交易,则无法完成3个完成和2个相反的操作。默认情况下,交易是整体完成或整体拒绝的。如果您需要更多的粒度,您将需要一种不同的策略,例如传奇模式但这可能是过分的。从链接的文档中:
简而言之:按顺序执行CRUD操作。如果失败了,您需要补偿/扭转先前执行的操作。如果这是第一个简单的,显然。操作 /故障点越多,它将变得越复杂。
If the 5 crud operations belong to the same transaction there is no way you can have 3 completed and 2 reversed operations. A transaction by default is either completed as a whole or rejected as a whole. If you need more granularity you will need a different strategy like the saga pattern but that might be overkill. From the linked doc:
In short: perform the crud operations in sequence. If one fails that you will need to compensate/reverse the previous executed operations. If it is the first one that is simple, obviously. The more operations / failure point the more complex it will become.