2

I have a .NET WinForms app. In it, I run 3 functions that do not do anything with a database. I simply want to roll back the actions of the functions if any fails.

So I'm looking for just a simple .NET transaction example, but everything I google seems to involve using the SQL transaction handling.

Thanks for any help.

user1761600
  • 233
  • 1
  • 5
  • 10
  • 2
    What kind of operations? – StingyJack Mar 29 '13 at 17:36
  • This may also be useful or pertinent to your question - http://stackoverflow.com/questions/1765615/transactions-for-c-sharp-objects – StingyJack Mar 29 '13 at 17:37
  • 2
    Depending on the details of your project, you could create equivalent undo functions for your methods and implement the Command pattern - http://www.codeproject.com/Articles/8303/Using-the-Command-pattern-for-undo-functionality – keyboardP Mar 29 '13 at 17:38
  • http://blog.paxcel.net/blog/extending-transaction-scope-to-non-database-transactions-using-resource-manager/ – BNL Mar 29 '13 at 17:41
  • 1
    Also consider the Memento pattern to save state about items for restoration - http://en.wikipedia.org/wiki/Memento_pattern. There are a few ways discussed in this thread - http://stackoverflow.com/q/8994433/16391 – StingyJack Mar 29 '13 at 18:04
  • For each of the three methods implement commit and rollback methods? But then where does it end? Do you have separate properties for dirty versus committed reads (gets)? Or do you have a single IsDirty property. What do the 3 functions do? – paparazzo Mar 29 '13 at 19:49

2 Answers2

0

Depends on what type of operations you want to roll back if the transaction fails. Generally you would need to implement one or more resource managers and enlist them in the transaction in order for it to work.

That's a very non-trivial undertaking, and it might be a sledgehammer where you need tweezers, i.e. you should probably not attempt it (I agree with the commenter who mentions the Memento pattern, that is worth looking into). In short, there is no way to simply "roll back" a method call since the changes that it makes might involve different types of resources (memory, filesystems, web services, etc.).

I think it's fraught with all sorts of logical difficulties as well, like should the transaction roll back just the changes performed by your method or should it also attempt to roll back changes performed by other methods called by your method? How would you enforce the ACID properties of the transaction? You would essentially have to stop any other threads from accessing the same memory locations during the transaction to prevent dirty reads, etc. Relational Databases are able to do this because they can strictly control which transactions can access what data and when. Whereas in a single process, when all the threads share the same address space, it is much more difficult.

If you're only interested in reversing memory operations, you should know that at one point MS was considering including Software Transactional Memory (STM) as part of the .NET Framework, but those plans were abandoned due to its difficulties. However, there's a very old MSDN article that tried to implement some similar functionality (it provides a Transactional<T> type for transactional access to values in memory). I've never seen this used in actual code though.

It might make an interesting research project. For anything else, bottom line though is don't do it. ;-)

luksan
  • 7,661
  • 3
  • 36
  • 38
0

You could look at implementing IEdtiableObject, another tutorial.

Echilon
  • 10,064
  • 33
  • 131
  • 217