Blue Theme Orange Theme Green Theme Red Theme
 
MindFusion's Components
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » ADO.NET & Database » Understanding ADO.Net Provider Factory in .Net 2.0

Understanding ADO.Net Provider Factory in .Net 2.0

This article describes the ADO.NET Provider Factory in .NET 2.0

Technologies: ADO.NET,Visual C# .NET
Total downloads :
Total page views :  23274
Rating :
 4.33/5
This article has been rated :  3 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
ArticleAd
Become a Sponsor



1. Introduction

ADO.NET class libraries are used for accessing data from a wide range of data sources. Its extensible nature allows the programmer to write their own implementation for accessing data from different data source i.e. programmer can write their own data provider API for accessing and manipulating data form custom data sources. ADO.Net class libraries in .Net Framework 1.1 allowed programmers to create their own data providers by implementing certain set of interfaces i.e. IDbConnection, IDbCommand, IDataReader, IDbDataAdapter etc.

If we closely look at some of the classes used to access data from a data source (lets say SQL Server) in framework 1.1, we can verify the interfaces implemented by these classes.

 

ADO.Net classes for accessing data from SQL Server Interface implemented
SqlConnection IDbConnection
SqlCommand IDbCommand
SqlDataReader  IDataReader
SqlDataAdapter  IDbDataAdapter

In Framework 1.1, if programmers want to implement a generic data access API, they have to use the interfaces instead of using provider specific classes. The provider information was mostly configured in the Config file based on which instances of the provider specific classes were created.

ADO.NET 2.0 provides newly defined factory classes which standardize the way programmer will create a provider-specific Connection. This simplifies the way programmers create provider specific instances in a generic data access API.

2. Inheriting Base Classes instead of Implementing Interfaces

In Framework 2.0, new base classes are introduced and each provider specific implementation derives from these base classes instead of implementing generic interfaces as in framework 1.1. This feature has a definite disadvantage as programmers cannot derive form multiple base classes, but this certainly gives much more flexibility to accommodate changes to the base class.

If we closely look at some of the classes used to access data from a data source (lets say SQL Server) in framework 2.0, we can verify the base class inherited by these classes

ADO.Net classes for accessing data from SQL Server Base class Inherited
SqlConnection DbConnection
SqlCommand DbCommand
SqlDataReader  DbDataReader
SqlDataAdapter  DbDataAdapter


With these changes the class hierarchy can be summarized in following diagram
Note: These base classes added in Framework 2.0 are abstract; hence they cannot be instantiated directly.


 
3. Looking at Provider Factory

Framework 2.0 exposes a class for instantiating the provider specific class in a generic data access API via something called Provider Factory. We have factory classes for each provider i.e. SqlClientFactory, OracleClientFactory etc. These classes are derived from a base class called DbProviderFactory and contain following methods:

  1. public virtual DbConnection CreateConnection();

  2. public virtual DbCommand CreateCommand();

  3. public virtual DbCommandBuilder CreateCommandBuilder();

  4. public virtual DbConnectionStringBuilder CreateConnectionStringBuilder();

  5. public virtual DbDataAdapter CreateDataAdapter();

  6. public virtual DbDataSourceEnumerator CreateDataSourceEnumerator();

  7. public virtual DbParameter CreateParameter();

  8. public virtual CodeAccessPermission CreatePermission(PermissionState state);

Method names are self explanatory about their usage; we will discuss some of these methods subsequently in this article.
DbProviderFactory for each available data provider is registered in the machine.config file as shown below.

<system.data>
<DbProviderFactories>
<add name="Odbc Data Provider" invariant="System.Data.Odbc" description=".Net Framework Data Providerfor Odbc" type="System.Data.Odbc.OdbcFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add name="OleDb Data Provider" invariant="System.Data.OleDb" description=".Net Framework Data Providerfor OleDb" type="System.Data.OleDb.OleDbFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add name="OracleClient Data Provider" invariant="System.Data.OracleClient" description=".Net Framework Data Providerfor Oracle" type="System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Providerfor SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</DbProviderFactories>
</
system.data>

Programmer can as well create a new entry in the machine.config file for a new data provider to expose the corresponding DbProviderFactory.

Another interesting point in above configuration file is the attribute invariant. This attribute represent the string, which can be passed to the GetFactory method of DBProviderFactories class to obtain a provider specific DBProviderfactory.

DBProviderFactories class is used to create provider specific DBProviderfactory. Various static method exposed by this class are described below.

  1. public static DataTable GetFactoryClasses();

  2. public static DbProviderFactory GetFactory(DataRow providerRow);

  3. public static DbProviderFactory GetFactory(string providerInvariantName);

Method names are self explanatory about their usage; Just notice that GetFactory method also accepts a string parameter, here we can pass the invariant name (as specified in the machine.config file) to obtain the provider specific DbProviderFactory. Also notice the first method which returns a DataTable, this can easily bind to a DataGridView control to display the list of available Data Providers to the users as described below

private System.Windows.Forms.DataGridView dataGridView1;
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
// Set Various properties of DataGridView
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = DbProviderFactories.GetFactoryClasses();
}

Note: With the introduction of Partial class, all the designer generated code will be a part of different file in VS 2005, so in above code the declaration of dataGridView1 and the initialization will be the part of designer generated code where as programmer has to write the Form1_Load code himself.

When we run the application we will see the following result.


 
When we run the application we will the following result.
In above application we can ask the user to select a specific row and then create the provider specific factory based on that as shown below

DbProviderFactory dbf = DbProviderFactories.GetFactory(dataGridView1.SelectedRows[0].Cells[2].Value.ToString());

Instead of above we can also pass the DataRow to the GetFactory() method as shown below

// Assuming that I want to create a factory for SQL Client
DbProviderFactory dbf = DbProviderFactories.GetFactory((DbProviderFactories.GetFactoryClasses()).Rows[3]);

Based on the exact requirement and usage scenario, programmer can use various options to create provider specific factory form DBProviderfactories class.

After obtaining the provider specific DBProviderFactory, the same can be used to create provider specific connections etc using the methods explained in section 3.

//Create connection from the DBProviderFactory
DbConnection con = dbf.CreateConnection();

Programmer can store the Connection string in the config file in following format

<
connectionStrings>
<add name="LocalSqlServer" connectionString="data source=...;Integrated Security= true;Initial Catalog=...;" providerName="System.Data.SqlClient" />
</
connectionStrings>

DBProviderFactory also exposes a method CreateDataSourceEnumerator which can be used to obtain all data source instances as described below.

dataGridView2.DataSource = dbf.CreateDataSourceEnumerator().GetDataSources();

CreateDataSourceEnumerator().GetDataSources() returns a DataTable which contains all the server names on the network along with instance name. It also provides some other information like IsClustered and Version etc.

Using this method, it's possible to configure the data source at runtime dynamically.

4. On an additional note

Framework 2.0 also introduces a new class called DBConnectionStringBuilder for building Connection Strings. This class implements interfaces like IDictionary and ICustomTypeDescriptor etc. DbConnectionStringBuilder is a base class for provider-specific connection string builders i.e. SQLConnectionStringBuilder, OleDbConnectionStringBuilder etc. DBConnectionStringBuilder class allows to specify specific properties for specific attributes in the connection string for example ShouldSerialize (Programmer can specify which all attributes in the connection string can be serialized.

5. How much generic code can be written?

Irrespective of using the provider factories, it's impossible to write complete generic code which will be provider independent. Programmers can make use of casting to expose various provider specific properties and methods i.e. by using code as shown below

if (con is SqlConnection)
//Do SQL Specific coding

Even though provider factories can be used by programmers to write code that might access different data sources, for example those writing data access tools or development environments; it's still an Option and not the way for all data access code to be written. Other issues must be considered, for example connection string formats and content vary for different data sources. The new classes use reflection to obtain an instance of the factory object, so this is a performance hit. However, this only has to be done once and not every time an object instance is required.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Amit Kumar Agrawal
Amit Kumar Agrawal is a Microsoft Certified Solution Developer (MCSD) in .NET and has been working in Microsoft Technologies for more than 4.5 years. He has extensive experience is designing and implementing Web Applications, Windows Applications and Distributed Applications. His areas of interest are Distributed Applications and Smart Device Applications.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On
connection poolingKim2/13/2007
what is pooling and its advantages
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved