Skip to content

SOLID — Abstract Factory, Strategy Pattern, Dependency Injection

May 18, 2013

During the last few weeks I’ve been working with Dependency Injection using Microsoft’s MEF as the DI container.  In the process I’ve been adhering to the SOLID principles as much as possible.  They greatly facilitate the use of Dependency Injection.  I’ve found that Mark Seeman’s book Dependency Injection in .NET, Copyright 2012 by Manning Publications, Shelter Island, NY is an excellent source of information concerning the key topics in Dependency Injection. 

Especially useful is Seeman’s overall “DI Perspective”: DI is not all about the container.  Rather DI is a broader topic, the goal of which is to produce higher levels of maintainability, testability, and extensibility in a code base.  This goal is achieved by applying the SOLID principles, which in turn are facilitated by Dependency Injection and the various containers available.

My general approach in my DI studies is to read some sections of Mark’s book, and then write some not-too-involved code to solve some problem I’ve encountered professionally using what I’ve learned from Mark.  Over the years I’ve found that the Strategy design pattern is extremely useful for encapsulating related algorithms.  For example, if one had to write software to do payroll calculations that was to be sold in multiple countries, the Strategy design pattern is a good choice to do the country specific payroll calculations for each country. 

This article uses the Strategy pattern for country specific payroll calculation.  It combines the use of the Strategy Pattern with the Abstract Factory pattern so as to be able to cleanly use Dependency Injection to program to an interface rather than to a concrete instance of a payroll calculation strategy class.  In the below example code I generally follow Seeman’s approach to the Abstract Factory, presented around page 165. 

The Visual Studio Solution for this article is on OneDrive in the file named AbstractFactory-ForumProblem-DI-MEF-RI.  Just click on this link, then click on the “Download” link on OneDrive’s top menu bar when it appears.  Be sure to do a “Clean” then “Build” before you run the solution.

If you want to know the details of the Strategy and Abstract Factory design patterns I encourage you to look them up online, or in Judith Bishop’s nice little book on all the Gang of Four design patterns C# 3.0 Design Patterns by O’Reilly.  Seeman’s also has a good write up of the Abstract Factory pattern on page 165.

The SOLID principles explicitly used in the above code are as follows:

  • Single Responsibility Principle – Used in all classes, as described at the top of each C# file.
  • Open Closed Principle – Used in the choice of a Strategy pattern for payroll calculations.
  • Liskov Substitution Principle – Used in the Strategy pattern.
  • Interface Separation Principle – Not explicitly used.
  • Dependency Inversion Principle – Used in the Strategy and Abstract Factory patterns, plus in all of the Dependency Injection that is done.

Please see other sources for explicit definitions of each SOLID principle.  Mark Seeman’s has a concise table on pp 284 – 285.

I have found that when my code follows the SOLID principles, thus being structured like the code in the accompanying solution, it is much, much less time consuming to extend and change code than when the code is structured in other ways.  My productivity goes way, way up when using SOLID principles.  Other’s I know have found the same thing.  Indeed, using the SOLID principles makes software development easier and much more fun!

The accompanying code has comments explaining the key things going concerning the use of MEF for DI, SOLID, and the Abstract Factory and Strategy patterns that implement Dependency Inversion (programming to an interface and not to a concrete instance of an object).  Many of the comment sections can be collapsed to get them out of the way of reading the code.  I will not walk you through the code in this article since the code is quite straight forward and the comments explain the key concepts.  I do suggest that you read the comments and also put a few break points in the code.  Especially put breakpoints in both of the classes that implement the IPayrollStrategy interface.

The start-up project should be the AbstractFactory-ForumProblem project.   Then you can just press F5 to run the solution.  But, be sure to do a “Clean” then “Build” before you run the solution.

I found that working this exercise did a great job of further imprinting in my mind how to write SOLID code, both logically and also intuitively – almost like the muscle memory one gets from repeatedly practicing a musical instrument or a sport.

Finally, note that the Abstract Factory is widely useful.  It can be used to produce very complex object graphs.  I’ve also found it useful to produce concrete instances of abstractions for WCF Proxies and WPF Popup Dialog Windows, among other things. 

Even more useful are Generic Abstract Factories that instantiate a type <T>, but that’s for another blog article.

I hope you find this exercise as helpful as I did.

George Stevens

Creative Commons License

dotnetsilverlightprism blog by George Stevens is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Based on a work at

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: