Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Insulate Your Code with the Provider Model

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




Application Security Testing: An Integral Part of DevOps

evelopers constantly face problems brought on by changing technologies. When Microsoft releases a new version of a data provider, or a customer decides to switch databases from Oracle to SQL Server, you may have to rework code you've already written. But 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.

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 run time. In this article, you will see how to create a data provider that lets you change from using SQL Server to a generic OLE DB provider with no code changes! You will just have to change a setting in a configuration file. Using the Provider Model gives you a great amount of flexibility and upgradeability to your source code.

Microsoft provides a set of Provider Model Templates that you can download from MSDN. The difference between their model and the one explained in this article is that Microsoft's templates are designed for use with web applications; in contrast, you'll see a UI-agnostic version here; you can use the same techniques for Windows Forms, ASP.NET, Windows services, web services, etc.

Before seeing how to implement a data provider, here's a look at three items that help you create a provider.

The Configuration Manager Class
You use the ConfigurationManager class from System.Configuration.dll to retrieve application settings from a configuration file. The 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 specifically for retrieving values from two built-in sections in .NET 2.0 configuration files: appSettings and connectionStrings. For example, suppose you have the following appSettings entry in a configuration file:

<appSettings> <add key="StateCode" value="CA" /> </appSettings>

You can use the following code to retrieve the StateCode value:

// C# ConfigurationManager.AppSettings["StateCode"]; ' Visual Basic ConfigurationManager.AppSettings("StateCode")

Similarly, you can retrieve entries from the connectionStrings section, such as the "Northwind" entry below:

<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.

// C# ConfigurationManager. ConnectionStrings["Northwind"].ConnectString; ' 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:

// C# IDataClass cust; Type typ; typ = Type.GetType("Customer"); x = (IDataClass)Activator.CreateInstance(typ); MessageBox.Show(cust.GetData()); ' Visual Basic Dim cust As IDataClass Dim typ As Type typ = Type.GetType("Customer") cust = CType(Activator.CreateInstance(typ), _ IDataClass) MessageBox.Show(cust.GetData())

The code above creates 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.

Editor's Note: This article was first published in the November/December 2007 issue of CoDe Magazine, and is reprinted here by permission.

Comment and Contribute






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



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date