C# AOP 事务方面

发布于 2025-01-18 15:28:29 字数 815 浏览 2 评论 0原文

我的问题与收到错误时所做的动作的撤消有关。

在我在.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难理解 2025-01-25 15:28:29

如果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:

Use the Saga pattern when you need to:

  • Roll back or compensate if one of the operations in the sequence fails.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文