Data Access Component and the Factory Design Pattern

Merely, every application today uses a relational database as its backend, applications used to be monolithic and structured, but with the need for more functionality and ease of use, applications became larger, more complex and harder to maintain, and the solution for rapid development became clearer 'Abstraction and Code Reuse'.


Applications are layered:


  • Presentation Layer
  • Business Layer
  • Data Access Layer 

A Data Access Layer usually provides the common mechanism and functionality for:


  • Opening and closing database Connections
  • Handling database Transactions
  • Executing SQL Commands, Data Readers and Data Sets

Back in the old days, when I started development, I used to write a data access class specific for the data provider embedded in the application, and then I used to copy the class from one application into another.


These were the old days and the bad habits, I started to realize how bad it were when I needed to write a VB.NET application where I had to transform the C# data access class into VB.NET then I had to transform both classes from the SQL provider to the Oracle provider and then again to the OLEDB provider and then things started to get really bad and messy, a lot of specific implementations sharing the same logic shattered across too many code files and classes.


Whenever I needed to add a new method, I had to write it down in too many places, whenever I needed to fix one line of code, I had to fix it in too many code files and not to mention that I tend to forget so I end up fixing the same bugs over and over again 'bad developer habits'.


Once I was talking to a friend of mine discussing the Factory Design Pattern and we ended the discussion with a practical Factory application applying it to Data Access Component as we realized that if we needed to support two or more different data sources for the same application, then we would only need to change the connection string not the Data Access Layer itself. By that time I realized the need to program to interfaces instead of specific data providers' implementation and that was my first real Object Oriented Programming lesson:


"Program to an interface and not to an implementation"


Another OOP approach:


"Favor object composition over inheritance"


Since that the data access component common functionality share the same logic but differs in the native Connection, Command, Transaction and DataReader that each data source provider provides, then it would be more practical and of more benefit to use an abstract class providing the common logic in public methods while providing abstract methods for each of the inherited data provider specific implementation to implement returning the native data source objects.


Design guidelines: "Use interfaces when you need classes that differ in implementation to have the same contract or protocol"


Included source code for a generic Data Access Component implementation written in C# that supports SQL, Oracle, OLEDB and ODBC data providers, using the factory design pattern for instantiating the specific data provider objects at run time based on the application configuration file or the caller defined data provider type.


Somebody is doing his homework and changing the bad habits with the good ones.




  • .Net Data Access Architecture Guide
  • Microsoft Data Access Application Block for .NET
  • Gamma, E., R. Helm, R. Johnson, and J. Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. 

Similar Articles