Click here to Skip to main content
13,344,508 members (48,148 online)
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 17 Jul 2004

Versatile and Extendible Data Access Factory

, 17 Jul 2004
Rate this:
Please Sign up or sign in to vote.
Using and extending the data access classes, and runtime selection and instantiation using a factory and XML configuration file.


This document describes how to use the Data Access Factory to create run-time objects that connect to and query Oracle or SQL Server. The demo project shows how the data provider can be changed without changing any code at all. The factory class is capable of creating any type of object you specify, as long as it inherits from the abstract class DataAccessBase. The factory gets its instructions on what type of object to create from an XML file specified by the developer and which is generally located in the project bin directory (for Window Forms projects) or in a virtual directory (for Web Applications). Currently, the application contains two derived classes of the DataAccessBase class: OracleDAO and SQLServerDAO. The assembly can easily be modified to include additional data providers such as Microsoft Access.

The core Data Access Factory is implemented along with classes for data access (DAO classes) in an assembly called DAL (Data Access Library). (The DAO classes mentioned should not be confused with earlier VB 6 nomenclature referencing RecordSet and other objects. They are un-related.) The demo project is available in a zip file by following the above link. The sample shows how to use the factory from a console application – use the demo to quickly learn how to use the factory.

The Data Access Factory assembly is useful for developers who want to code business logic or presentation logic without being concerned about what database vendor will be used. Without this type of logic, business logic code and presentation logic code are very difficult to de-couple from the objects used to access databases.

Additionally, clients of the Data Access Factory assembly can use multiple data sources.


I looked at the article Data Access and Transaction Handling Framework as a resource to use instead of what I developed, but decided not to use it because it used SQL statements as well as database object instantiation instructions all contained in the configuration file. I did not want that. Additionally, I wanted my database object instantiation to be setup differently than in that article. However, the current project is implemented as a factory, as is the project in the referenced article. If interested, you may want to consider evaluating that code as an alternative to using this code.

Using the code

The code block below shows how the sample application uses the DataAccessFactory and DataAccess classes.

//Define (declare and set memory aside) a factory object 
// that takes care of creating and returning
// a reference to the correct data access object type.

DataAccessFactory myDBFactory = new DataAccessFactory();

//Declare a variable capable of being any DAO object type
DataAccessBase myDBObj;

//Define a object (locally defined) that will help
// with the dirty chores of setting properties of
// the factory.  This object is not necessary, but just
// helps abstract out unnecessary details used in this
// particular test.
    factoryProperties = new factoryHelper();

    myDBFactory.ObjectTypeToCreate = factoryProperties.getDaoObjectType;
    myDBFactory.ObjectSetupIndex = factoryProperties.getIndex;
    myDBFactory.ConfigFileName = factoryProperties.getXMLFileName;
catch (Exception e)
    Console.WriteLine ("Problem in Factory Helper");
    Console.WriteLine ("Message: " + e.Message);
    Console.WriteLine ("Source: " + e.Source);

    //Use the factory to instantiate the appropriate type of
    // DAO object.  Assign the object to the base class object.

    myDBObj = myDBFactory.CreateVendorDAO();

    //Use the DAO object to generate a return a dataset based on
    // a user supplied query.
    string strSQL = "select * from customers";
    DataSet resultsDS = myDBObj.getDataSet(strSQL);
catch (Exception e)
    //Catch and report any problems.
    Console.WriteLine ("Found prblem.");
    Console.WriteLine("Error message: [{0}]", e.Message);
    Console.WriteLine("Source [{0}]", e.Source);


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

G Ryno
Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralNomenclature Pin
chibaken12-May-05 1:41
memberchibaken12-May-05 1:41 
Nothing fundamentally wrong with your code, but I have a comment to make about your use of DAO as the name of certain methods in your project - you may be aware that DAO was the precursor to Microsoft's current ADO data access infrastrucutre (back in the days of VS6). When I initially read your piece I thought you had written an old legacy DAO tool. As I said, not a biggy but if you go on to promote stuff like this...
GeneralTackles the easy problems Pin
Steven Campbell19-Jul-04 4:23
memberSteven Campbell19-Jul-04 4:23 
GeneralRe: Tackles the easy problems Pin
G Ryno19-Jul-04 12:37
memberG Ryno19-Jul-04 12:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 18 Jul 2004
Article Copyright 2004 by G Ryno
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid