【发布时间】:2010-07-15 04:12:45
【问题描述】:
在我当前的项目中,我正在使用实现以下ITransaction 接口的类,如下所示。这是可以撤消的事务的通用接口。我还有一个TransactionSet 类,用于尝试多个事务或事务集,最终可用于创建事务树。
ITransaction 的某些实现保留对对象实例或文件的临时引用,如果有对 Undo() 的调用,它可能会在以后使用。稍后可以确认成功的交易,之后不再允许Undo(),因此也不再需要临时数据。目前我使用Dispose() 作为我的确认方法来清理任何临时资源。
但是,现在我希望我的事务也触发事件以通知其他类发生了什么。除非交易得到确认,否则我不希望事件触发。因为我不想让事务通过撤消然后再次运行来多次触发事件。
既然我使用Dispose() 来确认交易,那么同时触发这些事件有什么问题吗?或者,除了清理临时数据的Dispose() 之外,在我的界面上有一个单独的Confirm() 方法来触发事件会更好吗?我想不出任何我想确认但不想处理交易的情况。然而,我并不完全清楚在 Dispose() 内我应该做什么和不应该做什么。
public enum TransactionStatus
{
NotRun, // the Transaction has not been run, or has been undoed back to the original state
Successful, ///the action has been run and was successful
Error //there was an attempt to run the action but it failed
}
/// <summary>
/// Generic transaction interface
/// </summary>
public interface ITransaction
{
TransactionStatus Status { get; }
/// <summary>
/// Attempts the transaction returns true if successful, false if failed.
/// If failed it is expected that everything will be returned to the original state.
/// Does nothing if status is already Successful
/// </summary>
/// <returns></returns>
bool Go();
/// <summary>
/// Reverts the transaction
/// Only does something if status is successful.
/// Should return status to NotRun
/// </summary>
void Undo();
/// <summary>
/// A message describing the cause of the error if Status == Error
/// Otherwise equal String.Empty
/// </summary>
string ErrorMessage { get; }
}
【问题讨论】:
标签: c# .net transactions idisposable