views:

32

answers:

1

I am using subsonic repository pattern(2.1) for asp.net mvc application.In my application,there are many repositories like categoryRepository,Blogrepository etc.Inside each of this repository i am calling subsonic's DB.Select().From()...ExecuteReader() and then loading domain objects from those reader.

In the controller action i make multiple calls from these repositories for e.g.

List<IBlog> blogs=_blogRepository.GetHottestBlogs();

List<ICategory> categories=_categoryRepository.GetAll();

do i have to implement any unitofwork pattern for this ?.My doubt is that how subsonic performs each operation DB.Update/Insert/Select .Is a TransactionScope is enough for batch update or do i have to use SharedDbConnectionScope to get better performance?

+1  A: 

With SubSonic you have to combine TransactionScope and SharedDbConnectionScope. Otherwise every command will use it's own dedicated connection which is disposed after execution, which will lead to an implicit commit (at least for MySQL and Sql Server I think).

Every SubSonic query within a SharedDbConnectionScope will share the same connection, so you can use the TransactioScope.

And you have to use the SharedDbConnectionScope before the TransactionScope, otherwise some providers won't detect that you are executing a Transaction.

using (new SharedDbConnectionScope())
{
    using (TransactionScope ts = new TransactionScope()
    {

        // Do some sh*i ;)
        ts.Complete();
    }
}
SchlaWiener
Thanks a lot for your response.I was trying to use TransactionScope before SharedDbConnectionScope .Thats how it is done in the unit tests shipped with the subsonic library.
ROHITH
Yeah, some time ago it was even wrong in the docs.However, it depends on how the DB Lib implements the TransactionScope. Afaik Sql Server looks for Transaction.Current only at creation of a SqlConnection while MySql supports TransactionScope to be used after creating a MySqlConnection. However, since SubSonic supports multiple DBMS it's always good to use the SharedDBConnectionScope first.
SchlaWiener