Click here to Skip to main content
Click here to Skip to main content

Building pluggable data access components

By , 25 May 2004
 

Introduction

In this article, I am going to explain how to build a set of data access components built on ADO.NET that can use different types of databases without many problems. When you build a database application, you will probably want to start off with a simple Microsoft Access database, but upgrade later to SQL Server or Oracle. In this case, you would have to completely rewrite your code before you can use the other types of databases. However, this isn't strictly necessary. You can always build the components so that they accept different types of databases. There are different approaches to solve this problem; I am going to explain just one way to build the data access components.

In this article, I am going to explain how to build data access components through the use of interfaces and a helper class. An interface provides us with a way to make calls to classes we don’t really know, but as long as they are built according to the interface, we can use them.

Multiple types of databases, same data

To make it possible to use different types of databases for the same application, you must first be sure that the databases deliver the same type of data to your application. After that, you can simply build the components.

The interface

The interface is, in this case, simple. I assume that there is only one table we want to link at the moment. So, we start off creating the interface like this:

public interface IDataAccess
{
 DataSet GetArticles();
 DataSet UpdateArticles(DataSet ds);
}

The interface simply implements 2 methods, one method for getting the data from the database, and another method for synchronizing the data with the database.

OLEDB connection

To connect this to OLEDB, we will need a Data Access Class for OLEDB. For easy use, we will create a new component and name that OleDbDataAccess. If you have Visual Studio .NET or SharpDevelop, you can then design the component by dragging an OleDbConnection and an OleDbDataAdapter to the designer. Configure them to use the database you made with Microsoft Access. After we designed the component itself, we go to the code and implement the interface. This could look something like this:

public class OleDbDataAccess: Component, IDataAccess
{
 public DataSet GetArticles()
 {
  DataSet ds = new DataSet();

  daArticles.Fill(ds);

  return ds;
 }

 public DataSet UpdateArticles(DataSet ds)
 {
    If(ds == null)
      return null;

    daArticles.Update(ds);
    return ds;
  }
}

The helper class

The helper class is nothing more than just a class that can spawn the right data access components for a given setting. The class implements a method GetDataAccessComponent which returns an instance of IDataAccess. The class looks like this:

public class DataAccessHelper
{
  IDataAccess GetDataAccessComponent()
  {
    string drivertype = ConfigurationSettings.AppSettings[“DriverType”];
    IDataAccess dal = null;

    switch(drivertype)
    {
      case “OleDb”:
        dal = new OleDbDataAccess();
        break;
      default:
        throw new NotSupportedException(“This drivertype is not supported”);
    }

    return dal;
  }
}

The method looks in the App.Config file of the application for an entry with the key DriverType. If the value of this entry is OleDb or any other valid driver type, a new instance of that data access component will be created and returned. If no valid driver type is found, the method will throw an exception.

Conclusion

You can add more Data Access components to your application that implement the interface IDataAccess. And change the switch statement in the GetDataAccessComponent method. I normally use OLEDB and SQL as my database types, but you can also use Oracle or ODBC if you like. As long as the data fits in a DataSet, you can use the interface.

Other implementations may be possible; this is the way I chose to use multiple databases with my applications. I am curious how other people have solved this problem, feel free to post a message with your idea :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

WillemM
Web Developer
Netherlands Netherlands
Member
WillemM is a 25 year old software developer working for Info Support. He loves new technology and spends most of his free time finding new ways to do things with his computer.
 
When not working on computers you can find him outside with his camera taking pictures.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralAccess database from C#.Net via ODBC PinmemberWill L Pittenger12 Oct '04 - 22:47 
GeneralRe: Access database from C#.Net via ODBC PinmemberWillemM12 Oct '04 - 22:53 
GeneralRe: Access database from C#.Net via ODBC PinmemberWill L Pittenger12 Oct '04 - 23:00 
GeneralRe: Access database from C#.Net via ODBC PinmemberWillemM13 Oct '04 - 5:16 
GeneralScaleability PinmemberSteven Campbell27 May '04 - 7:36 
GeneralRe: Scaleability PinmemberWillemM27 May '04 - 20:22 
GeneralRe: Scaleability PinmemberWillemM29 May '04 - 5:30 
Generalfactory design pattern PinmemberVeux27 May '04 - 0:53 
Hi,
good example, but what you researched for your work is already defined as "factory design pattern".
If you never heard about design patterns, have a look at them, they are very usefull.
 
Cheers Veux
 

GeneralRe: factory design pattern PinmemberWillemM27 May '04 - 3:24 
GeneralGood PinmemberPersian Guy26 May '04 - 17:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 26 May 2004
Article Copyright 2004 by WillemM
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid