views:

93

answers:

2

I've been exploring Sub Sonic 3's SimpleRepository and have been pretty happy with it but have a question regarding transactions. I am aware that using methods like 'AddMany' and 'DeleteMany' will automatically perform all of those operations within a single transaction, but was wondering if it's possible to force the SimpleRepository to perform the add or update of two different object types within the same transaction. For example, let's say I have the notion of two different but related entities in my application: a User and a Profile. Every user has to have a Profile and every Profile belongs to one and only one user. When a new user registers with my application I want them to provide the basic user information (credentials, name, e-mail) but also want some additional "profile" information (about me, gender, zip code, etc.) I'd like to be able to perform the add of the User and the Profile object within a single transaction, but since it requires two distinct calls to the 'Add' method with different type parameters I'm not sure how to make this work.

+1  A: 

You can do this using a transaction as follows:

  using (TransactionScope transactionScope = new TransactionScope())
  {
    using (SharedDbConnectionScope connectionScope = new SharedDbConnectionScope())
    {
      // Add your user
      // Add your profile

      transactionScope.Complete();
    }
  }
Adam
Thanks for the response. I had tried out TransactionScope before but got errors related to DTS. I was definitely missing the SharedDbConnectionScope to force connection sharing for the two data access calls. Now I'm getting an error as soon as I drop out of the connection scope using block. Looks like the Dispose method for SharedDbConnectionScope is causing some issue in this scenario. Stepping through the source now to try and track it down.
Jesse Taber
+1  A: 

adam - i think the code above is correct but is actually the wrong way around, it should be:

  using (SharedDbConnectionScope connectionScope = new SharedDbConnectionScope())
  {
    using (TransactionScope transactionScope = new TransactionScope())
    {
      // Add your user
      // Add your profile

      transactionScope.Complete();
    }
  }

cheers jimi

jimi
Thanks for the response, I'll try this out later.
Jesse Taber