Freigeben über


The 2009 Advent calendar situation

Assume that you have some important object that performs some important task. It may look something like this:

    1:      public class ImportantObject
   2:      {
   3:          public void ImportantMethod()
   4:          {
   5:              // Do things.
   6:          }
   7:      }

You've done everything right and created this object using BDD/TDD but one day everything changes. You need to make this object thread safe. The straight forward way to do this would look like this:

    1:      public class ImportantObject
   2:      {
   3:          private readonly object _lock = new object();
   4:   
   5:          public void ImportantMethod()
   6:          {
   7:              lock(_lock)
   8:              {
   9:                  // Do things.
  10:              }
  11:          }
  12:      }

But that is tricky to drive using specifications/tests, right? So over the next 24 days I'll show you a few ways to do this and by Christmas eve we should have something nice that works. And I'll be using xUnit.Net to write the specs/tests.