Introduction
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.
Background
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.
DataAccessFactory myDBFactory = new DataAccessFactory();
DataAccessBase myDBObj;
try
{
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);
return;
}
try
{
myDBObj = myDBFactory.CreateVendorDAO();
string strSQL = "select * from customers";
DataSet resultsDS = myDBObj.getDataSet(strSQL);
}
catch (Exception e)
{
Console.WriteLine ("Found prblem.");
Console.WriteLine("Error message: [{0}]", e.Message);
Console.WriteLine("Source [{0}]", e.Source);
}