Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Architecture
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
Bing
BizTalk
Book Excerpts
Build and Deploy
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Design Patterns
Development Process
Display Technologies
Distributed Computing
DotNetNuke
DSL
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
Internet Explorer 8.0
Interviews
iPhone
Iron Ruby
Java
Java Script
jQuery
LINQ
Linux
Mac OS X
MDX
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
Network
NHibernate
Object Oriented Development
Odata
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Python
Q&A
Rails
Rake
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Search
Security
Services
SharePoint
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
Tips
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WF
Whitepapers
Windows 7
Windows Azure
Windows Live
Windows Server
Windows Vista
WinForms
Workflow
WPF
XAML
XML
XNA
XSLT



DevConnections


 


QCon

Reader rating:
Click here to read 7 comments about this article.
Article source: CoDe (2007 Nov/Dec)


Article Pages:  1  2 3 - Next >


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

Click for a larger version of this image.

Figure 2: Sample application to retrieve data.

&

By: Paul Sheriff

Paul D. Sheriff is the President of PDSA, Inc. (www.pdsa.com), a Microsoft Partner in Southern California. Paul acts as the Microsoft Regional Director for Southern California assisting the local Microsoft offices with several of their events each year and being an evangalist for them. Paul has authored several books, webcasts, videos and articles on .NET, SQL Server and SharePoint. Paul can be reached via email at PSheriff@pdsa.com or at Paul Sheriff's Inner Circle (www.PaulSheriffInnerCircle.com).

PSheriff@pdsa.com

Fast Facts

Using the Provider Model gives you a great amount of flexibility and upgradeability to your source code.



Article Pages:  1  2 3 - Next Page: 'Loading the Data' >>

Page 1: The Provider Model
Page 2: Loading the Data
Page 3: DataLayer.CreateDataAdapter Method

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
4.1 out of 5

19 people have rated this article.

      Free Webinar

 

DevReach