Skip to main content

Provider agnostique - SQL Server, Oracle MySQL et autres

...ou comment réduire 2 jours de développement à 5 minutes sur un projet ADO.NET.

En regardant les webcasts des TechDays 2007 (auxquels je n'ai pas pu assister, puisque j'étais à Houston), j'ai été supris par la présentation de Pascal Belaud sur le provider MySQL (à 40 minutes du début). Comprenons-nous bien: je remercie Pascal de sa démonstration, et il connaît sûrement déjà ce que je vais expliquer ici.

Pascal nous explique que l'on peut maintenant faire appel à une base MySQL depuis du code ADO.net (j'en parlais courant décembre 2006), et que l'on peut facilement passer de MySQL à SQL Server. Et là, c'est la surprise: Pascal utilise du code typé MySQL dans lequel il vient remplacer "MySql" par "Sql" puis modifie la chaîne de connexion codée en dur dans le code, et recompile le tout. Plutôt fastidieux et pas vraiment adapté à une application qui doit pouvoir simplement fonctionner avec une base MySQL, SQL Server ou Oracle. Car pour faire cela il faut prendre Visual Studio et recompiler l'application.

Il existe une méthode autrement plus simple depuis l'avènement du framework .NET 2.0: la fabrique de provider, ou System.Data.Common.DBProviderFactory, qui permet de changer de provider en ne changeant qu'une chaîne dans le fichier de configuration XML de l'application (section connectionStrings).

Plutôt que d'en écrire une tartine, je vous renvoie vers la documentation de Microsoft à ce sujet et vous livre l'un de leurs exemples:

static DbProviderFactory dataFactory =
DbProviderFactories.GetFactory(
ConfigurationSettings.AppSettings["provider"]);

using(DbConnection conn = dataFactory.CreateConnection())
{
conn.ConnectionString =
ConfigurationSettings.AppSettings["connectionString"];
conn.Open();
...
}

Avec ce code, il suffit de modifier une ligne du fichier XML de configuration de l'application, et on change de type de base (i.e. de provider)!

Au final, c'est un excellent exemple du fait que la méconnaissance des dernières avancées du framework .NET peut faire perdre plusieurs jours sur un projet.

Comments

Popular posts from this blog

Gift idea : help your loved ones take their IT skills to the next level

You surely know a special person who works in IT. Learn Kubernetes & Docker enables them to quickly ramp up on those two technologies that one doesn't want to miss in 2021. With smoother operations, better scaling and availability, DevOps tooling, containers and containers orchestration smartly solve many problems that developers and system administrators commonly face. Don't let your loved ones face problems in their IT job : offer them the book that takes them from zero to productive in a matter of days. Grab it now! Get the ebook from Leanpub , Amazon or choose from many retailers . Get the print edition from Lulu .  

Learn Meteor book available

I'm pleased to announce the general release of my Learn Meteor book. It is now available as an ebook or print book from various sources: Learn Meteor print (paperback) on Lulu Learn Meteor ebook on LeanPub Learn Meteor ebook on Barnes & Noble Learn Meteor ebook on iBooks Learn Meteor ebook on Kobo Learn Meteor ebook on Scribd Learn Meteor ebook on Inktera Page Foundry Learn Meteor ebook on 24symbols Learn Meteor ebook on Amazon US Learn Meteor ebook on Amazon UK Learn Meteor ebook on Amazon France Learn Meteor ebook on Amazon Deutschland Learn Meteor ebook on Amazon Canada Learn Meteor ebook on Amazon India Learn Meteor ebook on Amazon Brasil Learn Meteor ebook on Amazon Mexico Learn Meteor ebook on Amazon España Learn Meteor ebook on Amazon Italia Learn Meteor ebook on Amazon Netherlands Learn Meteor ebook on Amazon Japan Learn Meteor ebook on Amazon Australia More sources are coming soon for the print version. Learn Meteor has been a fun expe...

Learn ASP.NET Core: read the book as it is being written

     TL;DR: get the discounted book now using the coupon , then get the full version for free. Thanks to the great acceptance of my previous books, ( Learn WPF MVVM ,  Learn ASP.NET MVC and Learn Meteor ), I'm in the process of writing a fourth one. Learn ASP.NET Core - MVC and DI with .NET Core 1.1 using Visual Studio 2017 teaches you how to quickly get coding using that technology. Just as I did previously, I'm publishing it before it is even finished. Would you like to read it? You can get it for half the price: 50% off the book using that coupon (limited to the first 100 readers and up to august 1st 2017): http://leanpub.com/netcore/c/K9mHH0IzfI2F About 50% of the book is already written, yet it is already available for download as an ebook: PDF, EPUB, MOBI: you choose your format. Over the next days I'll be writing the rest of the book, and publishing updates often. The final book will be ready by au...