在应用程序服务中使用 transactionscope

发布于 2024-11-13 05:58:07 字数 1248 浏览 4 评论 0原文

在我当前的支付网关应用程序中,我想执行此步骤

  1. 从 paypal 转账资金。
  2. 将付款记录保存在数据库中
  3. 按用户转账的金额增加用户的资金(在我们的数据库中)。

我正在应用程序层执行所有这些步骤。在事务范围内

下面是代码

 public void DepositFundInAdvertiser(PaymentInfo paymentInfo, RegistrationID advertiserRegistrationID)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                PaymentResult paymentResult = paymentService.DepositFund(paymentInfo);

                Advertiser advertiser = advertiserRepository.Find(advertiserRegistrationID);
                TransactionNumber transactionNumber = paymentRepository.NextTransactionNumber();
                Payment payment = PaymentFactory.NewPayment(advertiser.Person, transactionNumber, paymentInfo, paymentResult);

                paymentRepository.Save(payment);

                AdvertiserBalance newBalance = new AdvertiserBalance(advertiser.Balance.Amount + paymentInfo.PaymentTotal);//Increasing advertiser fund

                advertiser.AddFund(newBalance);

                advertiserRepository.Save(advertiser);

                scope.Complete();


            }

        }

问题:我的问题是我可以像这样在应用程序层中使用 Transactionscope,因为所有这些操作都应该是原子的?

In my current application in payment gateway, I want to perform this steps

  1. Transfer fund from paypal .
  2. Save the payment record in database
  3. Increase the user's fund(which is in our database) by the amount he transferred.

I am performing all this steps in app layer. Inside transaction scope

Below is the code

 public void DepositFundInAdvertiser(PaymentInfo paymentInfo, RegistrationID advertiserRegistrationID)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                PaymentResult paymentResult = paymentService.DepositFund(paymentInfo);

                Advertiser advertiser = advertiserRepository.Find(advertiserRegistrationID);
                TransactionNumber transactionNumber = paymentRepository.NextTransactionNumber();
                Payment payment = PaymentFactory.NewPayment(advertiser.Person, transactionNumber, paymentInfo, paymentResult);

                paymentRepository.Save(payment);

                AdvertiserBalance newBalance = new AdvertiserBalance(advertiser.Balance.Amount + paymentInfo.PaymentTotal);//Increasing advertiser fund

                advertiser.AddFund(newBalance);

                advertiserRepository.Save(advertiser);

                scope.Complete();


            }

        }

Problem : My question is can i use Transactionscope in app layer like this, because all this operation should be atomic ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

戏剧牡丹亭 2024-11-20 05:58:08

我想说这是复杂性和鲁棒性之间的一个很好的折衷。您只需记住,通过这样做您可以耦合两个存储库。如果不考虑这一更改对另一项和交易基础设施的影响,您将无法更改其中一项。只要两个存储库使用相同的数据库实例或完全支持分布式事务的 RDBMS,一切都很好。有点像。

您可能知道,纯粹的解决方案是异步消息传递,但它引入了很多复杂性。问题是,与异步消息传递相关的复杂性往往是平坦的——它不会随着您添加更多存储库、更多数据存储等而增加。从长远来看,它是有回报的。如果您有一个这样的用例,我会采用您提出的解决方案。正如我所说,这是良好的妥协,而制作软件就是良好的妥协。

I would say it's a good compromise between complexity and robustness. You only have to keep in mind that by doing this you couple both repositories. You won't be able to change either one without considering the effect of this change to the other one and to transaction infrastructure. As long as both repositories use the same database instance or RDBMS that fully supports distributed transactions, everything is fine. Sort of.

As you probably know, the pure solution is async messaging, but it introduces a lot of complexity. The thing is, the complexity connected with async messaging tends to be flat -- it doesn't grow as you add more repositories, more data stores etc. It pays off in the long run. If you have one such use case, I would go with the solution you proposed. As I said, it's good compromise and making software is about good compromises.

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