Archive for May, 2010

A C# Interface for Dependency Injection

May 27, 2010

This is a follow-up to Jesse Liberty’s Answering A C# Question blog post which compares two equivalent code examples to illustrate the value of interfaces:

  1. No interface example
  2. Interface with Dependency Injection (DI) example

Both examples use fictitious Notepad functionality with File and Twitter capability.

Example #1 does not use an interface and the line of code (LOC) count is 49.

Example #2 uses a Writer interface with Parameter Dependency Injection. The Notepad’s dependent objects (e.g., FileManager and TwitterManager) are passed as parameters (aka injected) to the worker method. In this case, the LOC count is 57.

It’s interesting to note that the interface example has slightly more code. The big win is less coupling which is much easier to maintain and more testable. I’ll have more about the testability in a future post.


Example 1 – No Interface

using System.IO;
using System;

namespace Interfaces
{
   class Program
   {
      static void Main( string[] args )
      {
         var np = new NotePad();
         np.NotePadMainMethod();
      }
   }

   class NotePad
   {
      private string text = "Hello world";

      public void NotePadMainMethod()
      {
         Console.WriteLine("Notepad interacts with user.");
         Console.WriteLine("Provides text writing surface.");
         Console.WriteLine("User pushes a print button.");
         Console.WriteLine("Notepad responds by asking ");
         Console.WriteLine("FileManager to print file...");
         Console.WriteLine("");

         var fm = new FileManager();
         fm.Print(text);

         var tm = new TwitterManager();
         tm.Tweet(text);
      }
   }

   class FileManager
   {
      public void Print(string text)
      {
         Console.WriteLine("Pretends to backup old version file." );
         Console.WriteLine("Then prints text sent to me." );
         Console.WriteLine("printing {0}" , text );

         var writer = new StreamWriter( @"HelloWorld.txt", true );

         writer.WriteLine( text );
         writer.Close();
      }
   }

   class TwitterManager
   {
      public void Tweet( string text )
      {
         // write to twitter
         Console.WriteLine("TwitterManager: " + text);
      }
   }
}

Example 2 – Writer Interface with Parameter Dependency Injection

using System.IO;
using System;

namespace Interfaces
{
   class Program
   {
      static void Main( string[] args )
      {
         var np = new NotePad();

         var fm = new FileManager();
         var tm = new TwitterManager();

         np.NotePadMainMethod(fm); // parameter injection
         np.NotePadMainMethod(tm); // parameter injection
      }
   }

   class NotePad
   {
      private string text = "Hello world";

      public void NotePadMainMethod(Writer w)
      {
         Console.WriteLine("Notepad interacts with user.");
         Console.WriteLine("Provides text writing surface.");
         Console.WriteLine("User pushes a print button.");
         Console.WriteLine("Notepad responds by asking ");
         Console.WriteLine("FileManager to print file...");
         Console.WriteLine("");

         w.Write(text);
      }
   }

   // Writer Interface
   interface Writer
   {
      void Write(string whatToWrite);
   }

   class FileManager : Writer  // Inherits Writer Interface
   {
      // Implements Write Interface Method
      public void Write(string text)
      {
         // write to a file
         Console.WriteLine("FileManager: " + text);
      }

      public void Print(string text)
      {
         Console.WriteLine("Pretends to backup old version file." );
         Console.WriteLine("Then prints text sent to me." );
         Console.WriteLine("printing {0}" , text );

         var writer = new StreamWriter(@"HelloWorld.txt", true);

         writer.WriteLine(text);
         writer.Close();
      }
   }

   class TwitterManager : Writer  // Inherits Writer Interface
   {
      // Implements Write Interface Method
      public void Write( string text )
      {
         // write to Twitter stream
         Console.WriteLine("TwitterManager: " + text);
      }
   }
}

Advertisements

Cloud Computing is an Overused Buzz-phrase

May 21, 2010

There are many meteorological pun’s associated with the term “cloud computing”. The term represents a huge paradigm shift in the way backend software services are delivered.

This article covers much of the confusion associated with this ambiguous and overused phrase.

From a software developer perspective, the deployment model and elasticity are the key differentiators for cloud services.

I consider a cloud service to be a system that can host my software and hide the complexity of the server farm (e.g., routers, load balancers, SSL accelerators, etc.).

Amazon popularized the term “Elastic Cloud” when they launched their core cloud component called EC2 back in August 2006. EC2 stands for Elastic Compute Cloud (EC2). Elasticity is the infrastructure’s ability to automatically scale up and scale down as needed.

Elasticity is a big deal. It dramatically simplifies the deployment and administration process. It means that software developers don’t need to worry much about infrastructure as much and can focus on coding the business process.

I consider Amazon, Google and Microsoft to be the big 3 cloud vendors. They have the elasticity expertise and server farms to support high volume cloud apps.

There’s Oracle, Salesforce.com, Rackspace and others but IMO are not generic cloud platforms.

For more about the non-developer cloud computing perspective, this Wikipedia article is a great reference.