views:

384

answers:

6

Hi All

I have a need to create a "transactional" process using an external API that does not support COM+ or .NET transactions (Sharepoint to be exact)

What I need to do is to be able to perform a number of processes in a sequence, but any failure in that sequence means that I will have to manually undo all of the previous steps. In my case there are only 2 types of step, both af which are fairly easy to undo/roll back.

Does anyony have any suggestions for design patterns or structures that could be usefull for this ?

Thanks

+2  A: 

The GoF Command Pattern supports undoable operations.

I think the same pattern can be used for sequential operations (sequential commands).

Thomas Owens
A: 

You might want to have a look at the Compensating Resource Manager:

(VS.80).aspx">http://msdn.microsoft.com/en-us/library/8xkdw05k(VS.80).aspx

Kev
+2  A: 

If your changes are done to the SharePoint object model, you can use the fact that changes are not committed until you call the Update() method of the modified object, such as SPList.Update() or SPWeb.Update().

Otherwise, I would use the Command Design Pattern. Chapter 6 in Head First Design Patterns even has an example that implements the undo functionality.

vitule
A: 

If you're using C++ (or any other language with deterministic destructor execution when scopes end) you can take a look at Scope Guards. This technique can probably also be adapted to .NET by making ScopeGuard implement IDisposable and sprinkling "using" statements as needed.

On Freund
A: 

Next to the GOF Command Pattern you might also want to have a look at the Transaction Script pattern from P of EAA.

You should probably create a Composite Command (or Transaction Script) that executes in sequence.

Huppie
+1  A: 

Another good way for rollback/undo is the Memento Pattern. It's usually used to take a snapshot of the object at a given time and let the object state to be reverted to the memento.

Cem Catikkas