The Provider Model (Cont.) DataLayer.CreateDataAdapter Method In the CreateDataAdapter method you will have to do a couple of things to create an instance of a specific data adapter. First you will need to initialize the appropriate provider based on the information in the configuration file. The InitProvider method is responsible for this and will be shown in the next section. After the appropriate DataProvider class is loaded the CreateDataAdapter method on that specific provider will be called. This is where the SqlDataAdapter or the OleDbDataAdapter or the OracleDataAdapter is created. In 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; }
In 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
DataLayer.InitProvider Method The InitProvider method is responsible for creating the actual provider object that will be used. To do this you first need a field/member variable to hold that data provider. You will create a variable named DataProvider that is of the type IDataProvider. Remember that the IDataProvider is the interface that each of the specific DataProviders that you create will need to implement. The first time the InitProvider method is called the Provider name will be loaded by reading the value from the configuration file, then you will use the System.Activator class to create a new instance of this provider. The DLL with the appropriate provider class must already be referenced by your project for this to work. In C# private static IDataProvider DataProvider = null;
private static void InitProvider() { string TypeName; string ProviderName;
if(DataProvider == null) { // Get provider name ProviderName = ConfigurationManager. AppSettings["ProviderName"]; // Get type to create TypeName = ConfigurationManager. AppSettings[ProviderName]; // Create new DataProvider DataProvider = (IDataProvider) Activator.CreateInstance( Type.GetType(TypeName)); } }
In Visual Basic Private Shared DataProvider As IDataProvider = _ Nothing
Private Shared Sub InitProvider() Dim TypeName As String Dim ProviderName As String
If DataProvider Is Nothing Then ' Get Provider Name ProviderName = _ ConfigurationManager. _ AppSettings("ProviderName") ' Get Type to Create TypeName = ConfigurationManager. _ AppSettings(ProviderName) ' Create new DataProvider DataProvider = _ CType(Activator.CreateInstance( _ Type.GetType(TypeName)), _ IDataProvider) End If End Sub
DataProvider.CreateDataAdapter Method Now you can finally look at the DataProvider class and its specific implementation of the CreateDataAdapter method. Look at the snippet below to see the class that uses the SqlClient.SqlDataAdapter. In C# class SqlDataProvider : IDataProvider { public IDbDataAdapter CreateDataAdapter() { SqlDataAdapter da = new SqlDataAdapter();
return da; } }
In Visual Basic Public Class SqlDataProvider Implements IDataProvider
Public Function CreateDataAdapter() _ As IDbDataAdapter _ Implements IDataProvider.CreateDataAdapter Dim da As New SqlDataAdapter
Return da End Function End Class
While this is a very simple provider method to write, it is necessary to implement it this way to provide the maximum flexibility and reusability. This becomes more apparent when you look at the other Provider class that uses the OLE DB namespace to create instances of OleDbDataAdapters. OLEDB DataProvider.CreateDataAdapter Method Below is another DataProvider class that uses the OleDb native provider. Notice that this code is almost exactly the same as the SqlClient-just the provider used differs. In C# class OleDbDataProvider : IDataProvider { public IDbDataAdapter CreateDataAdapter() { OleDbDataAdapter da = new OleDbDataAdapter();
return da; } }
In Visual Basic Public Class OleDbDataProvider Implements IDataProvider
Public Function CreateDataAdapter() _ As IDbDataAdapter _ Implements IDataProvider.CreateDataAdapter Dim da As New OleDbDataAdapter
Return da End Function End Class
Try it Out In the sample application that you can download for this article, try using each of the different providers provided to see how each one is called just by changing the value in the configuration file from OleDbDataAdapter to SqlDataAdapter. Step through the code to see where it creates an instance of the OleDb or SqlClient DataAdapters. As an exercise you could create additional providers that implement the OracleClient or any other native provider you are using. Conclusion Using a Provider Model will make the code you write much more generic, easier to maintain, and easier to upgrade as Microsoft (and other companies) introduce new technology. Other areas where you should use the Provider Model include Exception Management to determine where to publish exceptions. You could also use the Provider Model to determine where to read configuration settings from. You could have providers that read configuration settings from an XML file, the registry, a database table, or even a Web service. With a little imagination you can apply the concepts presented in this article to many areas of your application development process. Paul Sheriff |