在应用程序服务中使用 transactionscope
在我当前的支付网关应用程序中,我想执行此步骤
- 从 paypal 转账资金。
- 将付款记录保存在数据库中
- 按用户转账的金额增加用户的资金(在我们的数据库中)。
我正在应用程序层执行所有这些步骤。在事务范围内
下面是代码
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
- Transfer fund from paypal .
- Save the payment record in database
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说这是复杂性和鲁棒性之间的一个很好的折衷。您只需记住,通过这样做您可以耦合两个存储库。如果不考虑这一更改对另一项和交易基础设施的影响,您将无法更改其中一项。只要两个存储库使用相同的数据库实例或完全支持分布式事务的 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.