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.

[sourcecode language='xml']
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));
}
}

}
}
[/sourcecode]

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 {}.

[sourcecode language='xml']
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));
}
}

}[/sourcecode]

I like this and will use this from now on.


Andrew Chaa

another day, another PR