tags:

views:

169

answers:

6

Hi,

Can we use transaction on C# objects.

Does all the Transactions are only for Relational Database like MSSQL, ORACLE, etc...??

Thanks.

A: 

There is no SQL-like transactioning for c# objects, but if this because you have threading problems, you should use lock:

lock (myList)
{
    if (myList.Count > 0)
        myList.Clear();
}

This simply clears a list but ensures it can't be modified by another thread at the same time.

Codesleuth
+1  A: 

For a nice discussion how to implement transaction in OOP take a look at this discussion. There the memento and state pattern are highlighted to achieve transactional behaviour (among other methods).

spa
+1  A: 

You may be interested by Software Transactional Memory. It exists one implementation in the .NET world, named STM.NET.

Software Transactional Memory (STM.NET) is a mechanism for efficient isolation of shared state. The programmer demarcates a region of code as operating within a transaction that is "atomic" and "isolated" from other transacted code running concurrently.

Here is the blog of the Microsoft STM.NET team : http://blogs.msdn.com/stmteam/

Romain Verdier
You are right about STM, but don't forget to note that STM is still in research, development and I wouldn't use it in production software.
Steven
+1  A: 

What you are asking is called Software Transactional Memory. Since this is currently an open area of research and there is no native C# language support at the moment, you might be better off using the existing synchronization options for C#, such as the lock keyword, monitors, wait handles, etc.

If you really need advanced transactional features, there are a lot of library implementations, for example, <edit>SXM by Microsoft Research STM.NET</edit>.

Heinzi
A: 

Have a look at Software Transactional Memory (STM.NET).

It didn't make it into .Net 4 but is available as a separate download

adrianm
A: 

There was an Interesting article on Joe Duffy's blog about the attempt to do Transactional Memory at MS. If I were to sum it up, I would say: Only do this yourself if it is for fun.

Mads Ravn