Generic Data Access Layer in ADO

Embed Size (px)

Citation preview

  • 8/13/2019 Generic Data Access Layer in ADO

    1/25

    Implementing a Generic Data Access Layer in ADO.NET Part 1

    A Data Access Layer (DAL) is an integral part in the design of any application. There are plenty ofarticles that discuss how we an implement a DAL using ADO.NET. Most of these hae constraints inthe sense that they are not generic in nature. !n other words" they are not proider independent. Thisseries of articles will discuss the implementation of a generic" i.e." a proider independent Data AccessLayer in ADO.NET. The #asic prere$uisite to learning this article is a proper understanding of ADO.NET

    and good coding s%ills in &'. ! will present the code eamples in this article in &'. oweer with littleeffort" you can twist it oer to *+.NET as well.

    The ,trategies !noledLet us first understand what the necessities are for #uilding such a layer. ! would rather start #ydiscussing how an application designed using ADO.NET actually connects to the data#ase andperforms the &-D (&reate" -ead" pdate and Delete) operations.

    /irst" you need to open the connection using a data#ase proider. /ine" #ut what is a proider

    anyway0 A proider is responsi#le for connecting to a specific data#ase. 1hy specific0 The reason isthat a proider for an Oracle data#ase cannot #e used to connect to a ,2L ,erer data#ase and ice3ersa. Net" you need a command o#4ect that can #e used to eecute the data#ase commands of yourchoice. This is followed #y the usage of a Data-eader or a Data,et or a DataTa#le instance to retrieedata (if you are performing a -ead operation) from the data#ase ta#le. 1hen you use a Data,et" you

    need a DataAdapter as a #ridge #etween the actual data#ase and the Data,et instance.

    !mplementing the DAL /ramewor%1ith this in mind" let us design a proider independent Data Access Layer. Let us first understand theADO.NET Li#rary. The ma4or classes that constitute the ADO.NET li#rary are5

    &onnection

    &ommand

    Data -eader

    Data Adapter

    The corresponding interfaces that the a#oe classes implement are stated #elow.

    !D+&onnection

    !Data-eader

    !D+&ommand

    !D+DataAdapter

    The Data 6roiders that ma%e up the li#rary are specific to a particular data#ase that they wouldconnect to. These are the Data 6roiders that are aaila#le in ADO.NET.

    ,2L ,erer Data 6roider

    Oracle Data 6roider

    OD+& Data 6roider

    OleD+ Data 6roider

    Now we are all set to implement our DAL. The ma4or components that constitute our DAL #loc% are5

    6roiderType (Enum)

    Data#ase&onnection,tate (Enum)

    ,tored6rocedure6arameterDirection (Enum)

    D+Manager (&lass)

    D+elper (&lass)

    1e will start our discussion with the enum data type that would contain the data proider types in it.These proider types relate to the data#ases that we will #e connecting to" depending ourre$uirements. The following code snippet illustrates the 6roiderType enum that contains four aluesthat correspond to a specific data proider.

    pu#lic enum 6roiderType 7 ,$l,erer" OleD#" Oracle" OD+&" &onfigDefined

  • 8/13/2019 Generic Data Access Layer in ADO

    2/25

    8

    Now" there may #e situations where you might need to either %eep the data#ase connection stateopen or close after a data#ase operation is oer. As an eample" after you read data into aData-eader instance from the underlying data#ase" you might need to %eep the connection state openfor su#se$uent operations. 9ou may also need to close it if it is no longer used. :eeping this in mind"let us hae an enum data type that houses two alues that correspond to the data#ase connection

    states that we 4ust discussed a#out. The following is the code for the enum calledData#ase&onnection,tate.

    pu#lic enum Data#ase&onnection,tate 7 :eepOpen" &loseOnEit 81hen you are eecuting the stored procedures" you might want to send data to the data#ase orretriee the same from the data#ase. Accordingly" we hae another enum called

    ,tored6rocedure6arameterDirection that contains alues that correspond to the parameter directionsfor the stored procedures that we would eecute with the help of our DAL. The following is the codefor this enum.

    public enum StoredProcedureParameterDirection

    {

    Input, InputOutput, Output, ReturnValue

    }

    1e need a factory class that would return a D#6roider/actory type instance or a D#DataAdapter typeinstance depending on the data proider that we are using. This class contains factory methods thattypically are static methods. 1hat is a static method" anyway0 This is an often misunderstood concept#ut a ery important one. 1ell" a static method" often called a shared method (it is shared #y allinstances of the class that it #elongs to) #elongs to the class and a non3static method #elongs to ano#4ect of a class. That is" a non3static method can only #e called on an o#4ect of a class that it #elongsto. A static method can howeer #e called #oth on the class as well as an o#4ect of the class. /urther"a static method can access the static mem#ers of a class only unli%e a non3static method that canaccess #oth static and non3static mem#ers. These static methods in the D+/actory class accept a

    reference to the 6roiderType enum that denotes the data proider type in use.

    The net class in our discussion is the D+/actory class" designed on the factory design pattern. +eforewe discuss the D+/actory class and its intent" let us understand what a factory design pattern is. 1hatis a factory design pattern0 The /actory pattern is responsi#le for proiding an interface for thecreation of o#4ects" #ut allows the inherited classes to decide on the appropriate time of theseinstantiations.

    The following is the source code for our D+/actory class. !t contains two static methods called;et6roider and ;etDataAdapter #oth of which accept an instance of the data#ase proider typeenum" i.e." 6roiderType.using ,ystem.Data.&ommon