CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

David Hayden [MVP C#]

         .NET Tutorials, Patterns, and Practices

ADO.NET Provider Model Fundamentals

I hope everyone is having a happy Halloween.  Nothing makes Halloween more fun than my 3 children all under the age of 5.  We've gone to the local pumpking festival, the community halloween party, the county halloween party, and now comes the school halloween festival for my 5 year-old and trick-or-treating itself.  I love this holiday.

A good portion of the day has been spent reading Pro ADO.NET 2.0 and Pro ASP.NET 2.0 in C# 2005 on the lanai.  You can read about my Pro ADO.NET 2.0 Book Review.  The weather in Florida is perfect this time of year and almost makes up for the ever increasing threat of hurricanes.  Unbeliveable that we got to Hurricane Wilma and have now been dealing with named storms in the Greek alphabet.  Geez...

I am sure everyone and their brother is more up to speed on .NET 2.0 than myself as I have yet to do any real development with Visual Studio 2005 and Sql Server 2005.  Everything to date has been more playing on the computer and knowledge gained from a book.  However, the .NET 2.0 Provider Model is made up of the same old concepts that I have been discussing on this blog for some time, which is what I want to talk about in this post.

 

Focusing On Fundamentals

It is amazing to me that the more things change, the more they stay the same.  Most of the new concepts concerning the .NET 2.0 Providers follow the same design patterns and object-oriented principles that have been around for years.  The more you understand the basics and fundamentals of development, the quicker you grasp and understand the changes to the .NET Framework.

This provider model is the same concepts that we have experienced in the DotNetNuke Architecture, Community Server, Enterprise Library, etc.  I have been talking about these same principles of polymorphism, abstract base classes and factory method as discussed in Appyling UML and Patterns by Craig Larman and Agile Software Development Principles, Patterns, and Practices by Robert Martin for some time now.

 

DbProviderFactories

Actually the .NET Provider Model isn't new to .NET 2.0, but it has a new slant to it.  We now have this DbProviderFactory and DbProviderFactories classes that help us write more provider agnostic code for ADO.NET.  DbProviderFactory is an abstract base class for classes such as SqlClientFactory and OracleClientFactory.  Hence we can write something like this that gets us access to the SqlClientFactory:

 

DbProviderFactory provider =
    DbProviderFactories.GetFactory("System.Data.SqlClient");

 

The machine.config file on your PC registers four providers that are included with the .NET 2.0 Framework:

  • System.Data.Odbc
  • System.Data.OleDb
  • System.Data.OracleClient
  • System.Data.SqlClient

 

Provider-Agnostic Code

You can write provider-agnostic code by using the CreateXXX commands offered by the DbProviderFactory abstract base class:

  • CreateConnection()
  • CreateCommand()
  • CreateParameter()
  • CreateDataReader()
  • CreateDataAdapter()

 

Given a web.config file containing the following

 

<configuration>

    <connectionStrings>
        <add name="ProductCatalog"
            connectionString = "..." />
    </connectionString>
    
    <appSettings>
        <add key="factory"
            value="System.Data.SqlClient" />
        <add key="ProductQuery"
            value="Select * from Products" />
    </appSettings>

</configuration>

 

You can write some provider agnostic code like the following

 

string factory =
    WebConfigurationManager.AppSettings["factory"];

DbProviderFactory provider =
    DbProviderFactories.GetFactory(factory);
    
DbConnection connection =
    WebConfigurationManager.ConnectionStrings
    ["ProductCatalog"].ConnectionString;
    
DbCommand command = provider.CreateCommand();
command.CommandText =
    WebConfigurationManager.AppSettings
    ["ProductQuery"];
command.Connection = connection;

connection.Open();
DbDataReader reader = command.ExecuteReader();

// ...

 

Polymorphism and Factory Methods

All of these wonderful features boil down to Polymorphism and the Factory Method with the same signature

 

[ Polymorphic Abstract Base Class ] [ Instance Name ] = [ Factory Class ] . [ Factory Method ] ();

 

You will remember this signature from my discussions on the Enterprise Library Data Access Application Block:

 

Database db = DatabaseFactory.CreateDatabase();

 

found in the article:

 

In the case of the Enterprise Library example above, Database is an abstract base class and DatabaseFactory.CreateDatabase() initiates a concrete class derived from Database, SqlDatabase or OracleDatabase for example.  This is the same concepts happening with DbProviderFactory.

 

Conclusion

Everytime you turn around we are experiencing a new solution based on object-oriented principles that have been around for some time.  The best way to learn and come up to speed on these new techniques and technologies is to grasp the fundamentals of development.  Two good books that discuss the fundamentals in detail mentioned in this post are:

 

The ADO.NET Fundamentals discussed above can be found in the following two books:

 

Drinking:  Sencha Green Tea

 



Comments

Jason Haley said:

# November 1, 2005 8:05 PM

Christopher Steen said:

ADO.NET Provider Model Fundamentals [Via: dhayden ]
Ajax's responseXML Error [Via: ]

Anticipated...
# November 3, 2005 2:01 PM
Check out Devlicio.us!

This Blog

Syndication

News

CodeBetter.Com Home