Be a programmer

Live with passion....
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

The Provider Design Pattern and Data Access Component - I

Posted on 2006-04-20 16:23  Programmer  阅读(1398)  评论(1编辑  收藏  举报

The provider model is used throughout ASP.NET 2.0. It is a means of writing each of the technologies used so that new versions can easily be created and plugged in. For example, if you need to access a different database or authentication server, you can create a provider for it. ASP.NET 2.0 will then work with that provider just as it works with the existing features. This makes ASP.NET 2.0 much more flexible, expandable, and customizable than before. ---- MSDN ( Provider Toolkit )

Generally, an application can be devided into three layers at least: UI Layer, Logic Layer, and Data Access Layer. When designing an application, we should realize that our customers may use different data stores, such as Oracle, Microsoft SQL Server, MySQL, or even text files. Therefore it's very important that the Data Access Layer should be properly designed, since the other two layers of properly designed applications are rarely changed to accommodate new data stores. All what we need to do is just modifying the configuration file to tell our application to use the new Data Access Component. For example, the configuration settings for Microsoft SQL Server may look like:

  • <userManager defaultProvider="sqlProvider">
  • <providers>
  • <clear />
  • <add name="sqlProvider" type="Chenglin.Data.SqlUserDataProvider, Chenglin" />
  • </providers>
  • </dataProviders>
and for the MySQL,
  • <userManager defaultProvider="mySQLProvider">
  • <providers>
  • <clear />
  • <add name="mySQLProvider" type="Chenglin.Data.MySQLUserDataProvider, Chenglin" />
  • </providers>
  • </dataProviders>

ITransactionSupport & DataProviderBase

We should thus carefully design the Data Access Components. First, we declare an interface ITransactionSupport and a class DataProviderBase. The ITransactionSupport interface provides the transaction support for Data Access Components.

The DataProviderBase class implements the ITransactionSupport interface, so that we can easily perform transactions among various Data Access Components.

Then, it's very easy to perform transaction operations. For example: