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
B2B (Business Integration)
BizTalk
Book Excerpts
Build and Deploy
C#
C++
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
Graphics
Internet Explorer 8.0
Interviews
iPhone
Java
Java Script
jQuery
LINQ
Linux
Mac OS X
MDX
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
Network
NHibernate
Object Oriented Development
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
Podcasts
Post Mortem
PowerPoint
Print/Output
Product News
Product Reviews
Project Management
Python
Q&A
Reporting Services
REST
RIA Services
Ruby
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
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
Tips
UI Design
UML
User Groups
VB Script
VB.NET
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



TOWER 48


 


CODE TRAINING

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


Article Pages: < Previous - 1 2  3 


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

&


Article Pages: < Previous - 1 2  3 

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.3 out of 5

16 people have rated this article.

      CODE TRAINING

 

INSTANTLY dtSearch® TERABYTES OF TEXT