How does a code like TransactionScope work?

I read about the scope of transactions in C #, and it works as follows:

using (connectionDb)
{
    connectionDb.Open();
    using (var ts = new System.Transactions.TransactionScope())
    { <--
        try
        {
            connectionDb.ExecuteNonQuery();
            ts.Complete();
        }
        catch (Exception)
        {
            throw;
        }
        finally
        { }
    } <--
}

Each sentence in square brackets works with the same transaction, but I don’t understand how the code identifies that the database is working in a region without transferring the transaction region to either opening a connection or query execution.

For example, if I have the following code:

var myObject = new MyObject();
var childObject = new ChildObject();
childObject.Foo(myObject);
childObject.Bar(myObject);

Can I create a scope for a variable myObjectand use it in childObject methods without passing it a parameter? eg:

using(var myObject = new MyObject())
{
     childObject.Foo(); -- Here the method use the variable myObject
     childObject.Bar(); -- Here the method use the variable myObject
}
+4
source share
2 answers

TransactionScope Transaction.Current, . , , , . .

+7

. , . (, MSMQ), , , , . . 1

, , . - :

using(var myObject = new MyObject())
{
     childObject.Foo(myObject); -- Here the method use the variable myObject
     childObject.Bar(myObject); -- Here the method use the variable myObject
}

, , - , , , .

+1
source

All Articles