Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Insulate Your Code with the Provider Model : Page 3

Find out how to protect your code from changes by taking advantage of the Provider Model, which lets you swap components at run time.


advertisement
The IDataProvider Interface
As mentioned earlier, when you use the Provider Model you will need to create either an abstract base class or an interface that each provider class must inherit from or implement. This example uses an interface called IDataProvider.

Because data provider class implementations vary widely, an interface is the logical choice. There is no common code between the different data providers, so you can't use an abstract base class in this particular case. You can see the interface class in the code below:

// C# interface IDataProvider { IDbConnection CreateConnection(); IDbCommand CreateCommand(); IDbDataAdapter CreateDataAdapter(); } ' Visual Basic Public Interface IDataProvider Function CreateConnection() As IDbConnection Function CreateCommand() As IDbCommand Function CreateDataAdapter() As IDbDataAdapter End Interface

DataLayer.GetDataSet Method
Going back to the sample Windows Form code (reiterated below) you will see a call to the DataLayer.GetDataSet method. You call this method by passing in a SQL statement and a connection string. This method contains fairly standard ADO.NET code that creates a DataSet instance and uses a DataAdapter to fill that DataSet. It returns the filled DataSet, which is assigned to the DataSource property of the grid control:

// C# private void frmMain_Load(object sender, EventArgs e) { GridLoad(); } private void GridLoad() { string SQL = "SELECT * FROM Customers"; grdCust.DataSource = DataLayer.GetDataSet(SQL, AppConfig.ConnectString).Tables[0]; } ' Visual Basic Private Sub frmMain_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load GridLoad() End Sub Private Sub GridLoad() Dim SQL As String = "SELECT * FROM Customers" grdCust.DataSource = _ DataLayer.GetDataSet(SQL, _ AppConfig.ConnectString).Tables(0) End Sub

As you can see, by looking only at the form code you cannot tell which data provider is used to retrieve the code. It could be SQL Server, Oracle, or some OLE DB data provider. This works because the DataLayer class abstracts the specific code away from the UI layer. It's worth taking a look at the GetDataSet method in the DataLayer to see how it does its job.

The GetDataSet method itself does not use a specific provider such as SqlDataAdapter or OleDbDataAdapter. Instead it uses the interface IDbDataAdapter. The IDbDataAdapter is a .NET interface that anyone who writes a .NET native provider must implement when creating a DataAdapter class. You will find interface classes for each of the specific ADO.NET provider classes such as IDbConnection and IDbCommand:



// C# public static DataSet GetDataSet( string SQL, string ConnectString) { DataSet ds = new DataSet(); IDbDataAdapter da; da = CreateDataAdapter(SQL, ConnectString); da.Fill(ds); return ds; } ' Visual Basic Public Shared Function GetDataSet( _ ByVal SQL As String, _ ByVal ConnectString As String) As DataSet Dim ds As New DataSet Dim da As IDbDataAdapter ' Create Data Adapter da = CreateDataAdapter(SQL, ConnectString) da.Fill(ds) Return ds End Function

Instead of creating a specific instance of a data adapter, the preceding code calls the CreateDataAdapter method to perform this function. CreateDataAdapter (also contained within the DataLayer), loads the appropriate data provider class.

DataLayer.CreateDataAdapter Method
The CreateDataAdapter method must do a couple of things to create an instance of a specific data adapter. First it initializes the appropriate provider based on the information in the configuration file. The InitProvider method is responsible for this and will be discussed in the next section. After the appropriate DataProvider class is loaded the code calls the CreateDataAdapter method on that specific provider. That creates a specific adapter, such as SqlDataAdapter, or OleDbDataAdapter, or the OracleDataAdapter:

// C# public static IDbDataAdapter CreateDataAdapter( string SQL, string ConnectString) { IDbDataAdapter da; // Make sure provider is created InitProvider(); da = DataProvider.CreateDataAdapter(); da.SelectCommand = CreateCommand(SQL, ConnectString, false); return da; } ' Visual Basic Public Shared Function CreateDataAdapter( _ ByVal SQL As String, _ ByVal ConnectString As String) As IDbDataAdapter Dim da As IDbDataAdapter ' Make sure provider is created InitProvider() da = DataProvider.CreateDataAdapter() da.SelectCommand = CreateCommand(SQL, _ ConnectString, False) Return da End Function



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap