A Data Access Layer (DAL) is an integral part in the design of any application. There are plenty of articles that discuss how we an implement a DAL using ADO.NET. Most of these have constraints in the sense that they are not generic in nature. In other words, they are not provider independent. This series of articles will discuss the implementation of a generic, i.e., a provider independent Data Access Layer in ADO.NET. The basic prerequisite to learning this article is a proper understanding of ADO.NET and good coding skills in C#. I will present the code examples in this article in C#. However with little effort, you can twist it over to VB.NET as well.
The Strategies Involved in Creating a Data Access Layer
Let us first understand what the necessities are for building such a layer. I would rather start by discussing how an application designed using ADO.NET actually connects to the database and performs the CRUD (Create, Read, Update and Delete) operations.
First, you need to open the connection using a database provider. Fine, but what is a provider anyway? A provider is responsible for connecting to a specific database. Why specific? The reason is that a provider for an Oracle database cannot be used to connect to a SQL Server database and vice-versa. Next, you need a command object that can be used to execute the database commands of your choice. This is followed by the usage of a DataReader or a DataSet or a DataTable instance to retrieve data (if you are performing a Read operation) from the database table. When you use a DataSet, you need a DataAdapter as a bridge between the actual database and the DataSet instance.
Implementing the DAL Framework
With this in mind, let us design a provider independent Data Access Layer.
Let us first understand the ADO.NET Library. The major classes that constitute the ADO.NET library are:
- Data Reader
- Data Adapter
The corresponding interfaces that the above classes implement are stated below.
The Data Providers that make up the library are specific to a particular database that they would connect to. These are the Data Providers that are available in ADO.NET.
- SQL Server Data Provider
- Oracle Data Provider
- ODBC Data Provider
- OleDB Data Provider
Now we are all set to implement our DAL. The major components that constitute our DAL block are:
- ProviderType (Enum)
- DatabaseConnectionState (Enum)
- StoredProcedureParameterDirection (Enum)
- DBManager (Class)
- DBHelper (Class)
We will start our discussion with the enum data type that would contain the data provider types in it. These provider types relate to the databases that we will be connecting to, depending our requirements. The following code snippet illustrates the ProviderType enum that contains four values that correspond to a specific data provider.Continues…