I WANT ONE.
(via juliatheoctopusqueen)Source: skullsvilleusa
A while ago I started writing about transactional behavior in SharePoint Today I’m gonna go a little bit more deeper in the matter.
So if we do a little bit of abstraction on a transactional operation, we find that there are two basic operations: do something and undo that which we did.
Lets be a little more elegant: lets call those two operations Execute() and Undo(). Let’s start then, with a simple interface:
With a little bit more on thought on the matter, we can separate transactions on two groups: a single operation or a set of operations. Since we are doing transactions on SharePoint, I propose atomic transactions to have and SPListItem and an SPWeb, and those which are a collection of operations to have additionally a collection of transactions.
The atomic transaction is pretty simple:
We have a couple of constructors and a couple of abstract operations to be implemented on the child classes, and the SPListItem and SPWeb objects to work with.
The transaction collection, on the other hand, is a little bit more complex:
Yes, there is a Stack object there to store the operations. Let suppose for a moment that you are executing a large batch of operations and something fails. You need to start undoing stuff beginning with the last operation and ending with the first operation. Yes we need a LIFO data structure, and the Stack provides this kind of behavior.
So now that we have our base classes, we can begin writing some transaction implementation for our base Sharepoint operations: create, update and delete.
Since there is NO WAY in this world to recover an Item after doing a SPListItem.Delete(), so instead I’m going to use Recycle(), which allows us to recover stuff from the SharePoint Recycle Bin.
After digging up a little bit how the recycle works we find that after recycling an element we get a Guid which identifies the element on the bin. So our delete command will have a Guid as a field so we can recover the element from the bin in case something fails…
The SPUpdate, and SPCreate commands are on the visual studio solution, which can be grabbed from here. The SPCreate is a nice sample of a CommandCollection, so remember to give a look to it.