The Provider Model In this article you will learn how to isolate yourself from change by taking advantage of the Provider Model. Designing your applications using the Provider Model will allow you to swap components out at runtime, thus allowing you to upgrade them easily. Developers face the problem of constantly changing technology. When Microsoft releases a new version of a data provider, or a customer decides to switch databases from Oracle to SQL Server, this can cause you to have to rework a lot in the code you’ve already written. You can avoid much of this rework if you take the time to plan and code for such changes. One recommended way to do this is to develop components that take advantage of the Provider Model. | " | Microsoft provides a set of Provider Model Templates that you can download from their Web site. The difference between their model and the one that I will explain in this article is that Microsoft’s are really designed for Web applications. The method I show is UI agnostic.
| " |
A provider is a class or a component that provides specific functionality to an application. However, the Provider class used will not be known until runtime. In this article, you will learn how to create a data provider that will allow you to change from SQL Server to an OLE DB provider with no code changes! You will just have to change a setting in a configuration file. Microsoft provides a set of Provider Model Templates that you can download from their Web site at http://msdn2.microsoft.com/en-us/asp.net/aa336558.aspx. The difference between their model and the one that I will explain in this article is that Microsoft’s are really designed for Web applications. The method I’ll show is UI agnostic. This means that you can use the same technique in Windows Forms, ASP.NET, Windows services, Web services, etc. Creating a Provider To build a provider you need to take advantage of a few technologies available in .NET. Essentially you’ll perform these four steps: Before you learn how to implement a data provider, you need to look at three of the items that help you create a provider. The Configuration Manager Class The ConfigurationManager class, located in the System.Configuration.dll, is used to retrieve application settings from a configuration file. This configuration file can be a Windows Forms configuration file or a Web.config file in an ASP.NET Web application. ConfigurationManager replaces the old ConfigurationSettings class from .NET 1.1. The ConfigurationManager class contains two properties that are designed for specifically retrieving values from two built-in sections in .NET 2.0 configuration files; namely AppConfig and ConnectionStrings. So given the following entry in a configuration file: <appSettings> <add key="StateCode" value="CA" /> </appSettings>
You can use the following code to retrieve the StateCode value: In C# ConfigurationManager.AppSettings["StateCode"];
In Visual Basic ConfigurationManager.AppSettings("StateCode")
If you have the following entry in the configuration file: <connectionStrings> <add name="Northwind" connectionString= "Server=Localhost;Database=Northwind; Integrated Security=True"/> </connectionStrings>
You can use the following code to retrieve the Northwind connection string. In C# ConfigurationManager. ConnectionStrings["Northwind"].ConnectString;
In Visual Basic ConfigurationManager. _ ConnectionStrings("Northwind").ConnectString
Abstract Base Class or Interface You use an abstract base class when you have a class that can implement some or most of the functionality of the classes that will be inheriting from it, but the inheriting class must provide the actual implementation. In other words, the class that inherits from the abstract base class will do some of the work and the abstract base class will do some of the work. You use an Interface when there is no common code that could be put into a base class. In this case, you use an Interface so each class has a list of standard methods and properties that whatever consumes that class can rely on being there and being implemented. System.Activator Class Sometimes in an application you do not know what class to load until run time. This is normally due to a data-driven scenario where the name of the class is placed into a database table or in a configuration file as a string. Your application then needs to use this at run time to create an actual instance of a class. To do this, you can use the System.Activator class to build an object from a string. The example below shows how to dynamically create an instance of an object at run time. In C# IDataClass cust; Type typ; typ = Type.GetType("Customer"); x = (IDataClass)Activator.CreateInstance(typ);
MessageBox.Show(cust.GetData());
In Visual Basic Dim cust As IDataClass Dim typ As Type
typ = Type.GetType("Customer") cust = CType(Activator.CreateInstance(typ), _ IDataClass)
MessageBox.Show(cust.GetData())
In the code above you create an instance of a Customer class. This code assumes that the Customer class either inherits from an abstract base class or implements an Interface named IDataClass. Building a Data Provider To illustrate the points outlined so far in this article you can create a data provider to use SQL Server, OLE DB or the Oracle native providers based on settings in a configuration file. The advantage of this approach is your User Interface layer will only ever call the DataLayer class for all DataSets, DataReaders, commands, etc. The DataLayer class will ensure that the appropriate provider is used based on settings in the Configuration file (Figure 1). Sample Application To test out this model you can create a sample Windows Form application with a GridView control on a form that will load the Customers table from the Northwind database (Figure 2).  Figure 2: Sample application to retrieve data. |