nested using staments

I love using statement for database access. It closes the connection automatically when all the codes within the using statment are executed. Recently I had to nest using staments for the project I was working on, and there were guys who thought it wasn’t elegant. The arguments were, first, they doubted if nesting using actually work. Second, it did not conform to the standard.

using (RecruiterUser uen = new RecruiterUser())
{
    using (RecruiterUserSite userSite = new RecruiterUserSite())
    {
        IDbTransaction trans = uen.BeginTransaction(scnfg);
    
        try
        {
            uen.update(trans, fd.memId, fd.comId, fd.Forename, fd.Surname, fd.Email);
            userSite.updateSite(trans, fd.memId, fd.SiteId, 0, 0, false, DateTime.Now,
                fd.ExpiryDate, 0, 0, 0, 0);
    
            userSite.CommitTransaction(trans);
        }
        catch (Exception e)
        {
            try
            {
                userSite.RollbackTransaction(trans);
                PublishException(new BaseEx(scnfg, "Update fd failed and is rolled back", e));
            }
            catch (Exception ex)
            {
                PublishException(new BaseEx(scnfg, "Rollback Update fd failed", ex));
            }
        }
    
    }
}

In my opinion, coding standard is important in team play, but also it should not hinder our progress in coding. We developers grow each day in our insight in the code. We need to be able to update the coding standard, rather than the standard drag us behind.

For the beauty of nested using, I agree that it does not look elegand. I googled a littlbe bit, and found that there were may other guys who thought about the beauty. Their solution was to nest using without {}.

using (RecruiterUser uen = new RecruiterUser())
using (RecruiterUserSite userSite = new RecruiterUserSite())
{
    IDbTransaction trans = uen.BeginTransaction(scnfg);

    try
    {
        uen.update(trans, fd.memId, fd.comId, fd.Forename, fd.Surname, fd.Email);
        userSite.updateSite(trans, fd.memId, fd.SiteId, 0, 0, false, DateTime.Now,
            fd.ExpiryDate, 0, 0, 0, 0);

        userSite.CommitTransaction(trans);
    }
    catch (Exception e)
    {
        try
        {
            userSite.RollbackTransaction(trans);
            PublishException(new BaseEx(scnfg, "Update fd failed and is rolled back", e));
        }
        catch (Exception ex)
        {
            PublishException(new BaseEx(scnfg, "Rollback Update fd failed", ex));
        }
    }

}

I like this and will use this from now on.

nested using staments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s