August 2007 Entries

Trying Out Persistence Ignorance with LINQ (Part II)

In Part I, we defined an IUnitOfWork interface to avoid coding directly to the System.Data.Linq.DataContext class. IUnitOfWork gives us the opportunity to manipulate IDataSource<T> objects. IDataSource<T> is another abstraction. When the software is running in earnest, we need to back an IDataSource<T> with a table in a SQL Server database. However, during unit testing we might prefer to have an IDataSource<T> backed by an in-memory data structure. Essentially, we want the ability to switch between a System.Data.Linq.Table<T> implementation (SQL Server) and a System.Collections.Generic.List<T> implementation (in-memory) - without changing the code and LINQ expressions in the upper layers of software....

What’s Wrong With This Code (#17)

We interrupt this LINQ series with an emergency! Well, there is no real emergency, but there hasn’t been a WWWTC for some time, so … The following program is suppose to compress its own source code into a Program.cs.zip file, then reverse the compression and produce a Program.txt file. The problem is: Program.txt always shows up as an empty file! What’s wrong? using System; using System.IO; using System.IO.Compression; namespace YippyZippy {     class Program     {         static void Main(string[] args)         {             Compress(@"..\..\Program.cs", @"..\..\Program.cs.zip");             Decompress(@"..\..\Program.cs.zip", @"..\..\Program.txt");         }         private static void Compress(string inFileName, string outFileName)         {             FileStream inStream = File.Open(inFileName, FileMode.Open);             FileStream outStream = File.Open(outFileName, FileMode.Create);             GZipStream zipStream = new GZipStream(outStream, CompressionMode.Compress);             try             {                 byte[] buffer = new byte[inStream.Length];                 inStream.Read(buffer, 0, buffer.Length);                 zipStream.Write(buffer, 0, buffer.Length);             }             finally             {                 outStream.Close();                 inStream.Close();                           }         }         private static void Decompress(string...

Trying Out Persistence Ignorance with LINQ (Part I)

In June, Ian Cooper wrote an article (Being Ignorant With LINQ to SQL) that provided marvelous details about using POCOs with persistence ignorant repositories. Ian's conclusion: "LINQ to SQL is usable with a TDD/DDD approach to development. Indeed the ability to swap between LINQ to Objects and LINQ to SQL promises to make much more of the code easily testable via unit tests than before." I've been tinkering along the same lines as Ian, and although I think I started working at the other end of the problem first, I'm reaching the same conclusions (and ended up with much of...

Handling Faults in Windows Workflow Custom Activities

Here is one I had to track down recently. The code in the following activity is trivial, but FaultyActivity represents any activity that might throw an exception during Execute and overrides HandleFault (which the WF runtime will schedule to run when it catches the exception tossed up by Execute). public partial class FaultyActivity: Activity {    public FaultyActivity()    {       InitializeComponent();    }     protected override ActivityExecutionStatus Execute(                         ActivityExecutionContext Context)     {         // something went wrong         throw new InvalidOperationException();     }     protected override ActivityExecutionStatus HandleFault(                         ActivityExecutionContext context,                         Exception exception)     {         // something still went wrong         throw new InvalidOperationException();     } } The problem is that once an activity enters the Faulting state, it can only transition to Closed or back to Faulting. If another exception...

Images That Strike Fear In A Developer's Heart

Anytime you open up a design tool, and see something like this ... ... you know you'll need stiff drink at the end of the day. It doesn't matter if the boxes represent tables or classes – it's a complicated mess. In this case, the screenshot is from a Visual Studio 2005 Analysis Services project. Anyone have some aspirin to spare?

What's Wrong With This Well?

Water . When you don't have it - you can find out who your true friends are. Your true friends are the ones who open the door when you are standing outside with a towel, toothbrush, razor, and clean pair of underwear in hand. Last week I turned the tap at the kitchen sink and .... nothing happened. Plumbing is far beyond my area of expertise, so I called for professional help. The plumber poked around and decided everything from the well pump to the kitchen tap was in perfect working order. Like any good debugging session, there was only...