如何建立与运作的TransactionScope类?TransactionScope

2023-09-07 11:58:14 作者:有你的夜晚群星闪耀

只是想知道,如果我想创建做了一个类,我希望能够在一个TransactionScope使用,那么我需要实现?

这就是:我的类需要知道,这是在一个事务中,但如何将它获得的提交或回滚通知?而就回滚,我将​​如何实际回滚?

我想我的类将有一个像添加,更新和删除只修改变化的临时列表和方法读,这需要检测,如果它是在一个事务中,并返回修改方法或未经修改的数据。因此,后来我需要一种方法提交/回滚被调用不知何故?

请问我订阅Transaction.TransactionCompleted事件?如果是的话,我该如何避免多次预订同一交易?

我注意到,交易没有标识,是有办法管理/多个并发交易或嵌套事务指指点点?

MSDN文档 System.Transactions的有很多内容,但它似乎是针对消费者,而不是执行者,所以我不知道是否有人有一个很好的来源(无论是在网上还是在一本书),就如何服务将提供交易支持?

让我们假设我的类不具有已支持事务,并​​能够只是通过传递一个基本的商店。假设我的课是这样的:

 公共类MyClass的{
    私人列表<为MyObject> _businessData;

    公共无效创建(MYOBJECT数据){...}
    公共MyObject来读(查询字符串){...}
    公共无效更新(MYOBJECT数据){...}
    公共无效删除(MYOBJECT数据){...}
}
 
简介与运行原理

解决方案

这篇文章有什么是需要一个很好的概述。这是旧的,但我相信这一切仍然适用。

要总结文章中,您需要调用的争取方法上的交易类,传入的实施 IEnlistmentNotification

Just wondering, if I want to create a class that does something and I want to be able to be used in a TransactionScope, what would I need to implement?

That is: My class needs to be aware that it's in a Transaction, but how would it get notified on Commit or Rollback? And on Rollback, how would I actually Rollback?

I assume my class would have methods like "Add", "Update" and "Delete" which only modify a temporary list of changes, and a method "Read" which needs to detect if it is in a transaction and return modified or unmodified data accordingly, but then I need a method Commit/Rollback that gets called somehow?

Would I subscribe to the Transaction.TransactionCompleted event? If yes, how do I avoid multiple subscriptions to the same transaction?

I noticed that Transactions do not have IDs, is there a way to manage/juggle with multiple concurrent transactions or nested transactions?

The MSDN Documentation for System.Transactions has a lot of content but it seems to be aimed at consumers rather than implementors, so I wonder if someone has a good source (either on the web or in a book) on how a service would provide support for Transactions?

Let's assume that my Class does not have an underlying store that already supports transactions and is able to just "pass it through". Let's assume my class looks like this:

public class MyClass {
    private List<MyObject> _businessData;

    public void Create(Myobject data) { ... }
    public MyObject Read(string query) { ... }
    public void Update(Myobject data) { ... }
    public void Delete(Myobject data) { ... }
}

解决方案

This article has a good overview of what is required. It's older, but I believe it all still applies.

To summarize the article, you need to call one of the Enlist methods on the Transaction class, passing in an implementation of IEnlistmentNotification.