views:

115

answers:

5

I was wondering if there is a way to have a transaction on object. I'm asking this question for the following situation:

We pass our object to our dataaccesslayer. There we use transactionscope to make sure the database doesn't get corrupted.

In that process an object can be changed (datechanged, owner, ect ...). But if the transaction fails the transactionscope rollbacks the transactions in the database but not on your object.

This way your object (which is passed by reference) is kinda corrupted.

I hope there is a solution for this.

+2  A: 

When you use TransactionScope, it will explicitly tell you when it rolls back (by giving you an error). If this happens, you shouldn't continue to process on your object until you've got it clean from its source.

Sohnee
A: 

I really doubt you can have transaction on your class objects. Maybe you could customize your code to maintain the state in another instance and restore it just after your rollback statement.

Rashmi Pandit
that is the memento pattern.
Sem Dendoncker
A: 

I think I might have found some solution. It's not as easy as a transactionscope but it could do the trick. It's called the memento pattern: http://en.wikipedia.org/wiki/Memento%5Fpattern

Sem Dendoncker
+2  A: 

what about this http://stackoverflow.com/questions/1765615/transactions-for-c-objects

Aneef
A: 

I you use a good OR-Mapper like NHibernate instead of a custom data layer, this will be taken care for you

irfn