Thursday, December 18, 2008

Unit Testing - Distributed Transaction Coordinator

Unit Testing error while working with Transactions for the Database.

While testing the using "(var transaction = new TransactionScope())" method stated in a previous unit testing blog, I ran into an error that the code in my class could not access my local DB.

I was shown that I need to run the Distributed Transaction Coordinator. This is found under the Windows Services and can be set to automatic.

It's working fine now.

ASP.NET - Page Life Cycle

Page life cycles must be used in the correct matter.

One thing I found working with legacy systems is to make sure you use the page life cycle correctly. There are some good cheat sheets on this - I might add it in here later.

To read up on this on the MSDN site go here.

Dictionaries - KeyValuePair

I needed to use a better collection.

I was working on a legacy framework using HashTables. My colleague suggested to rather use Dictionaries (since they are serializable). I then stumbles over this article explaining how using KeyValuePair is faster than using the regular Keys in a foreach loop. Read more here

Here's an example:
foreach (KeyValuePair pair in d)
{
Console.WriteLine(pair.Key + ", " + pair.Value.ToString());
}

Unit Testing - Database Entries

I needed to unit test some DB entries.

I got this code sample from a colleague of mine.

[TestMethod()]
public void SaveThisTest()
{
using (var transaction = new TransactionScope())
{
//do the target creation
//do the Assert(s)
//dont call transaction.Complete(); //Not calling it will cause a "rollback"
}
}

And that's it... read more here
(this guy has a different point of view, but I have not tried it)