pauloya wrote:
I can see that you don't advice us to use TransactionScope the way we do. Until now we didn't find any problems related to the transaction behaviour, we just had the Distributed Transaction dependency problem.
But is there a possibility to have a coding solution where we can define a scope for a transaction and not worry about adding entities to the transaction inside every method , just like TransactionScope allows with implicit transactions?
The TransactionScope doesn't really do anything, it just sits there collecting votes what to do with the transactions it controls. So all transaction started inside a scope are governed by it, but that leads to a distributed transaction if you have multiple ado.net transactions. To avoid that, you have to pass around the 'Transaction' instance (the instance of the generated Transaction class) and add the entities to persist to that. When you're done, commit that transaction.
That's making sure you use 1 transaction and don't get distributed transactions. You then also don't need the scope.
Maybe by creating our own class and adding custom code to the LLBL generated classes?
Thanks.
You could use a static variable somewhere marked with [ThreadStatic] (to avoid multi-threaded usage) which is the transaction object to use and use that in all your code, but that's IMHO leading to less maintainable code, as you rely on a static global variable for transaction management, which could lead to problems if that static parameter got changed. But on the other hand it might suit your needs.
But in sort, what you are after, an indirect way to do things in some hidden transaction, that's not possible/present, you always have to actively add the entities to the transaction like you should, so things get executed in the active transaction.
You could create a scope like class for this though, which contains the static variable (marked with TreadStatic) which creates a scope for Transaction. In your code, simply obtain the static variable from the scope class, if it's not there, create a local Transaction object, otherwise use that ThreadStatic marked one. This way you don't have to pass around the Transaction instance and you can control with a scope class where which transaction is used.
If you need help with this, please let us know.