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);
      }
   }
}

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.

2010: The Year and Decade for 4 Screens and a Cloud

January 4, 2010

Rob Enderle’s blog post has it right. 2010 will be the year and start of the cloud decade.

I’d like to take it a step further. The coming wave of ubiquitous ‘democratized’ data services with eager clients waiting to consume will take the internet to a dramatic new level. Microsoft’s three screens and a cloud vision speaks to it but I believe its more about “4 screens with data services”. I consider the data services to be more relevant. The cloud is the engine but the 24/7 data services it provides will be life changing/business transforming.

Thanks to 3G, pending 4G and whatever comes after, the data services will come from highly reliable mobile data pipes that can be consumed while driving a car, riding a bicycle, at the doctor’s office or exercising at the gym.

The data services are democratized because the data being provided was once only available to a select few. Opening up the data to software developers and entrepreneurs can be a catalyst for positive change. The democratization of data trend is an unstoppable force that has the power to accelerate innovation to help solve some of the world’s problems and improve the quality of life for all.

The US Chief Information Officer, Vivek Kundra, understands the power of democratized data. He spearheaded a new web site for this called Data.gov. Another great example is the City of New York’s recent NYC Big Apps Contest. Microsoft is also getting involved with their new Dallas service.

Regarding the 4 screens, not 3, I expect the data services to be designed to support the following clients.

 

1. Large Screen 10 foot away living room experience.

2. Desktop/Tablet Screen Multi-touch Tablet and PC monitor
experience.
3. Small Screen Smartphone
(e.g., Blackberry, Palm,
iPhone, iPod Touch, Android,
Win Phone 7, ZuneHD, etc.)
4. Car Dashboard Screen This is the Ford Sync, Fiat Blue&Me,
Kia UVO and General Motors OnStar.

Listing the Car Dashboard may be a bit premature but I expect to see at least 25 million "connected" cars sold during this coming decade. In less than a year, the Microsoft Ford Sync system has already exceeded 1 million in US only sales. These systems are just starting to go global with Kia’s UVO and Fiat’s Blue&Me systems. I expect “Connected Cars” consuming mission critical data services to become the norm within 5 years.

Examples of the mission critical and revenue generating data services are the real-time location-aware contextual ads or electronic billboards. Some of this is already available in the Ford Sync system. I consider it the first commercially viable Augmented Reality solution. I expect Car Dashboard solutions to eventually provide windshield “heads up display” driving directions that can also show the nearest movie listings, nearest Thai restaurants, closest hospitals, etc.

Aside from the Car Dashboard services, data services will come in many flavors. The more popular services will be the entertainment and news services:


Video Netflix, Hulu, Youtube, Boxee
Music iTunes, Pandora, Zune
Games Xbox Live, SONY Playstation Network
Books Amazon Kindle, Nook, PDF, Audible
News NY Times, CNN, MSNBC, ABC, CBS and all of the Radio News Feeds
Sports ESPN

 

There will be Quality of Life services such as:

  1. Health medical record services – HealthVault
  2. Real-time Traffic – Calculate Quickest Travel Time
  3. Air/Pollen Quality – What will the air/pollen be like on December 31st at 5:30 PM.
  4. Population Growth versus Food Supply – Expected food supply in Somalia over the next 3 years.
  5. Malaria Cases/Birth Rates/Life Expectancies by Region
  6. Violence Levels in Iraq and by Region
  7. Airport Security Wait Times – Security Check Wait Time at Gate #4 in LAX, etc.
  8. Crime Stats by Region
  9. High School Education Quality by Region

 

The list of potential services is endless.

Much of this data is already available but is not in a format that can be easily used or consumed by the 4 screens mentioned.

I’ll leave it to the developers and entrepreneurs to pioneer.

Ten years from now, I am confident that we’ll all be grateful for this new cloud computing/data services era.

 

Microsoft PDC09 Connected Show Podcast #21

December 31, 2009

Wanted to thank Peter Laudati and Dmitry Lyalin for having me on Connected Show Podcast #21 with Sara Chipps recently.

We covered a lot of ground during the podcast and Peter posted all of the relevant links.

There was a lot of great news at PDC09 but I was most fascinated by Microsoft’s support for building Java Apps with Windows Azure.

For the Java Developers out there, here’s the “Building Java Apps with Windows Azure” session.

 

Technorati Tags: ,,,,

PDC09 Predictions/Expectations

November 17, 2009

In a few hours the 2009 PDC will officially kick off. For software developers, every PDC keynote has its share of surprise announcements. There was a rumor this morning about some Windows Mobile 7 related announcements coming this week but I doubt it.

I think Microsoft is not ready to discuss Windows Phone 7 (WP7) publicly yet. My guess is they’re shooting to make WM7 announcements on January 6th, 2010 at the CES 2010 keynote event.

NeoWin sometimes gets the inside scoop and they say Microsoft will discuss plans for IE9 and Silverlight 4 in the morning.

Regardless of surprise, the big news is the Dawn of Microsoft’s Cloud Era. It also sets a tone for Google and Amazon. Microsoft will disclose all details on their cloud strategy in the morning. Financial analysts will be listening in carefully so they can tweak their MSFT revenue forecast models.

I predict/expect Ray Ozzie to rock the house in the morning.

It’ll be a busy 3 days. I’m expecting deep dives in the following areas.

  1. IE9
  2. Silverlight 4
  3. Office Web 2010 – Is it better than Google Docs?
  4. Microsoft Web Platform – http://www.microsoft.com/web/
  5. Multi-Touch Everywhere – desktops, tablets, surface computers, phones, cars, TVs, game consoles
  6. Facebook/Twitter Everywhere – desktops, tablets, surface computers, phones, cars, TVs, game consoles
  7. Visual Studio 2010 – Intellitrace, Coded UI Tests/Silverlight Test Automation
  8. Language Futures – Parallelism, Dynamic and Procedural, Dependency Injection/IOC
  9. Security Services – Windows Identity Foundation (formerly Geneva)
  10. Cloud Computing Architectures – Hybrid, Service Bus, Big Table
  11. ASP.Net MVC 2 and 2.5
  12. IIS7 and beyond – http://www.iis.net/

For more info, follow this live blog in the morning.

Microsoft’s Azure Cloud Full Court Press starts November 17

October 31, 2009

The full court press to get developer’s onto the Microsoft Azure Cloud starts Tuesday, November 17th at 8:30 AM PST at the Microsoft Professional Developer Conference. I’ll be there and will be blogging as it unfolds.

I believe it’ll take at least 2 years for Azure to get the sales and usage momentum needed to be successful. As you may know, the 3 big cloud vendors; Amazon, Google and Microsoft are getting their stakes in the ground (er… cloud).

This month’s Forbes Magazine covers Microsoft’s efforts here.

It’s interesting to note that Azure will be half the cost of Amazon but Google App Engine beats them all.

The cloud computing transition is going to be big. The success of Microsoft’s Azure as well as Google and Amazon is inevitable.

I wonder how old school data center companies like AboveNet and Equinix will survive.

2009 Mobile App Development Contests

August 11, 2009

The next 20 years of software development is all about Fat Client / Fat Cloud Mobile Apps.

For the software developers, I’m restating the obvious. Let’s face it, the days of thin client / fat server are over. We quickly moved to a new world of ubiquitous mobile computing everywhere (e.g., in the car, on the train, at a baseball game, in a plane). I have software running in my watch, my sneakers, my bicycle, toaster, refrigerator and bathroom scale. The devices communicate peer to peer or device to cloud.

Thanks to the iPhone and iPod Touch, the buzz for the foreseeable future is the new small screen computing devices. I started working on some mobile apps recently and was surprised to see the level of developer interest.

I’m a big fan of Google’s Android largely for the development community and the great development tools. Google and Microsoft understand that the success of the platform depends on a healthy and vibrant development community. In an effort to measure the current ‘success’ of a platform’s development community, I compared the annual developer contests of the 6 major mobile phone vendors.

If you rank the 6 vendors by cash prize award, Android, Win Mobile and Blackberry come out on top. The following table has the details.

2009 Mobile App Developer Contests

#

Mobile
Phone
Contest

Submission
Date

Award
Date

1st Prize

2nd Prize

3rd Prize

1

Android

Android Developer Challenge

8/31/2009

Nov 2009

$150,000

$50,000

$25,000

2

Win Mobile

Windows Mobile Developer Contest

9/30/2009

Oct 2009

$100,000

$50,000

$20,000

 

3

Blackberry

Blackberry Developer Challenge

9/25/2009

Nov 10, 2009

$100,000

$20,000

$5,000 to 16 finalists

4

iPhone

Apple Design Awards

5/4/2009

Jun 12, 2009

$10,000 in hardware and other perks

-

-

5

Palm Pre preDevCamp Developer Challenge

8/9/2009

Aug 2009

Devices and $200 Gift Card

-

-

6

Nokia

Nokia Developer Programs

-

-

-

-

-

 

Dell Latitude XT2 Tablet PC Unboxing

May 9, 2009

My 64 bit Dell Latitude XT2 arrived this week.

It came pre-installed with Vista 64 Business Edition. I have since reinstalled the OS to use the latest 64 bit Windows 7 Ultimate RC with new 64 bit N-Trig drivers and so far so good. I need more time with it but the pen stylus and multitouch are surprisingly very responsive.

It looks like the processor horsepower is very good. It should be good enough for my software development needs. I’m going to install Visual Studio 2008 Team Suite and Android/Eclipse later this week. I’ll let you know how it goes.

In the mean time, here’s the unboxing photos.

1. Main Box

IMAG0126

2. Inside Main Box

IMAG0127

3. Inside Accessory Box

IMAG0129

4. Secondary Box containing the XT2 Tablet PC

IMAG0134

5. Contents of Secondary Box with the XT2 Tablet PC pulled out

IMAG0135

6. XT2 Tablet PC Box Opened

IMAG0131

7. First view of XT2 Tablet PC

IMAG0136

8. XT2 Tablet PC Unwrapped

Dell-Latitude-XT2_2

9. XT2 Tablet PC next to older HP zd7000

Dell-Latitude-XT2

Invoice for 64 bit Dell Latitude XT2 ordered on Friday – April 10th, 2009

April 22, 2009

I placed an order for the 64 bit Dell Latitude XT2 on Friday, April 10th.

It looks like there’s a 5 week turnaround as the estimated delivery date is May 18th.

I’ll have the un-boxing as soon as it arrives.

Here’s the invoice. It includes the 24 inch UltraSharp monitor.

# Item Description Price
1 224-3594 Latitude XT2 Non-TAA Base $4,099.00
2 311-9873 Latitude XT2, Intel Core 2 DuoSU9400, 1.40GHz, 800MHz, 3M L2 Cache, LED LCD $0.00
3 311-9884 5.0GB DDR3, SDRAM, 2 Dimms (1GB Integrated) Latitude XT2 $0.00
4 330-2802 Internal English Keyboard for Latitude XT2 Notebooks $0.00
5 320-6270 Dell UltraSharp 2408WFP,Wide Flat Panel w/Height AdjustableStand,24.0 Inch VIS,OptiPlex Precision and Latitude $0.00
6 320-7678 Intel Integrated Graphics Media Accelerator 4500MHD Latitude XT2 $0.00
7 341-8949 128GB Dell Mobility Solid State Drive for Latitude XT2 $0.00
8 468-2071 Vista Business 64-BIT Service Pack 1, with media, English Latitude $0.00
9 430-3364 Dell Wireless 365 Bluetooth Module, Latitude XT2 $0.00
10 330-2840 90W , 3-Pin,AC Adapter for Latitude XT2 $0.00
11 330-0879 US – 3-FT, 3-Pin Flat E-FamilyPower Cord for Latitude E-Family $0.00
12 330-2831 MediaBase with 8X DVD+/-RW for Latitude XT2 $0.00
13 420-9184 Cyberlink Power DVD 8.1,with Media,Dell Latitude/Mobile Precision $0.00
14 420-8010 Roxio Creator Dell Edition,9.0Dell Latitude/Mobile Precision $0.00
15 430-3086 Dell WLAN 1510 (802.11a/b/g/n 2X3) 1/2 MiniCard for LatitudeE/Mobile Precision $0.00
16 330-0884 No Intel vPro Technologys advanced management features for Latitude, Mobile Precision $0.00
17 330-2843 Resource DVD with Diagnostics and Drivers for Vista LatitudeXT2 Notebook $0.00
18 312-0852 6-Cell/42 WHr Primary Battery for Dell Latitude XT2 $0.00
19 467-6457 Energy Star 4.0 Enabled, EPEATGOLD, Latitude XT2 $0.00
20 993-4248 Dell Hardware Limited Warranty Plus Onsite Service Extended Year(s) $0.00
21 993-4247 Dell Hardware Limited Warranty Plus Onsite Service Initial Year $0.00
22 989-3449 Thank you choosing Dell ProSupport. For tech support, visit http://support.dell.com/ProSupport or call 1-866-516-3115 $0.00
23 992-2102 ProSupport for End Users: Next Business Day Parts and Labor Onsite Response 2 Year Extended $0.00
24 992-5740 ProSupport for End Users: Next Business Day Parts and Labor Onsite Response Initial Year $0.00
25 983-7572 ProSupport for End Users: 7×24 Technical Support and assistance for end-users, 2 Year Extended $0.00
26 984-3980 ProSupport for End Users: 7×24 Technical Support and assistance for end-users, Initial $0.00
27 900-9987 Standard On-Site Installation Declined $0.00
28 310-8319 Intel Core 2 Duo Processor $0.00
29 310-8758 You have chosen a Windows Vista Premium System $0.00
30 310-8977 Info SKU-Software and Peripherals products and solutions catalog included in system boxes $0.00
    Sub-total: $4,099.00
    Shipping: $0.00
    Tax: $343.29
    Order Total: $4,442.29

Is there a “Windows Cloud Home Server” coming soon?

February 6, 2009

I bought this Windows Home Server (WHS) recently.

I consider it a necessity for the home office.

The key features are:

  1. Centralized Backup
  2. File Sharing – NAS device
  3. Remote Access Gateway – remote access to any connected PC on your home network via Internet.
  4. Printer Sharing – Centralized print server.
  5. Shadow Copy – “Point in time” snapshots to recover older versions of files.
  6. Headless Operation – No monitor or keyboard. Use remote desktop or remote admin client tool.
  7. Media Streaming – Can stream media to Xbox 360 or other devices using Windows Media Connect.
  8. Data redundancy – Data is stored across multiple drives.
  9. Expandable Storage – has 4 drive bays. Two drive bays are in use, two are empty.
  10. Extensibility through Add-Ins – can host IIS web apps.
  11. Health Monitoring – Track health of all PCs on the home network (e.g., antivirus, firewall, etc.)
  12. Server Backup – backup the backup.

 
Some Positives:

· Fully Automated Minimal maintenance required except for the Server backup.
· Quiet Quiet enough for the bedroom/living room.
· Ease of Use Easy to use backup and restore process.

 

Some Negatives:

· No RAID The system uses proprietary Windows Home Server Drive Extender technology. There’s a good post on why it does not support RAID here.
· Complicated High ease of use for technologists but still too complicated for low tech folks.
· Unwanted extensions HP included two Add-Ins to WHS, PVConnect and McAffee Anti-virus. I don’t use either.
· Annoying Prompts McAffee annoyingly prompts to buy full service.
· Cloud Backup Need easy and low cost way to back up server to a cloud service such as Amazon S3.

Overall, I consider this product to be an important milestone for home computing.

However, I don’t think it will ever become mainstream until there’s zero config / zero maintenance.

I also believe that as cloud computing becomes pervasive and as costs drop, WHS will eventually be offered as a full blown cloud service with no onsite backend hardware required.

If so then Microsoft may call it Windows Cloud Home Server or even Windows Live Home Server.


Follow

Get every new post delivered to your Inbox.