• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

收藏:如何定制ASP.Net2.0的Provider

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Contents

 

Introduction to the Provider Model
Membership Providers
Role Providers
Site Map Providers
Session State Providers
Profile Providers
Web Event Providers
Web Parts Personalization Providers
Custom Provider-Based Services
Hands-on Custom Providers: The Contoso Times

ASP.NET 2.0 includes a number of provider-based services for reading, writing, and managing state maintained by applications and by the run-time itself. Developers can write services of their own to augument those provided with the system. And they can make those services provider-based to provide the same degree of flexibility in data storage as the built-in ASP.NET providers.

There are three issues that must be addressed when designing and implementing custom provider-based services:

  • How to architect custom provider-based services
  • How to expose configuration data for custom provider-based services
  • How to load and initialize providers in custom provider-based services

The sections that follow discuss these issues and present code samples to serve as a guide for writing provider-based services of your own.

Architecting Custom Provider-Based Services

A classic example of a custom provider-based service is an image storage and retrieval service. Suppose an application relies heavily on images, and the application's architects would like to be able to target different image repositories by altering the application's configuration settings. Making the image service provider-based would afford them this freedom.

The first step in architecting such a service is to derive a class from ProviderBase and add abstract methods that define the calling interface for an image provider, as shown in Listing 1. While you're at it, derive a class from ProviderCollection to encapsulate collections of image providers. In Listing 1, that class is called ImageProviderCollection.

Listing 1. Abstract base class for image providers

public abstract class ImageProvider : ProviderBase
}

The next step is to build a concrete image provider class by deriving from ImageProvider. Listing 2 contains the skeleton for a SQL Server image provider that fetches images from a SQL Server database. SqlImageProvider supports image retrieval, but does not support image storage. Note the false return from CanSaveImages, and the SaveImage implementation that throws a NotSupportedException.
Listing 2. SQL Server image provider

[SqlClientPermission (SecurityAction.Demand, Unrestricted=true)]
public class SqlImageProvider : ImageProvider

SqlImageProvider's Initialize method expects to find a configuration attribute named connectionStringName identifying a connection string in the <connectionStrings> configuration section. This connection string is used by the RetrieveImage method to connect to the database in preparation for retrieving an image. SqlImageProvider also accepts an applicationName attribute if provided but assigns ApplicationName a sensible default if no such attribute is present.

Configuring Custom Provider-Based Services

In order to use a provider-based service, consumers must be able to configure the service, register providers for it, and designate which provider is the default. The Web.config file in Listing 3 registers SqlImageProvider as a provider for the image service and makes it the default provider. The next challenge is to provide the infrastructure that allows such configuration directives to work.

Listing 3. Web.config file configuring the image service

<configuration >
   
  
<connectionStrings>
    
<add name="ImageServiceConnectionString" connectionString="" />
  
</connectionStrings>
  
<system.web>
    
<imageService defaultProvider="SqlImageProvider">
      
<providers>
        
<add name="SqlImageProvider" type="SqlImageProvider"
          connectionStringName
="ImageServiceConnectionString"/>
      
</providers>
    
</imageService>
  
</system.web>
</configuration>

Simce <imageService> is not a stock configuration section, you must write a custom configuration section that derives from System.Configuration.ConfigurationSection. Listing 4 shows how. ImageServiceSection derives from ConfigurationSection and adds two properties: Providers and DefaultProvider. The [ConfigurationProperty] attributes decorating the property definitions map ImageServiceSection properties to <imageService> attributes. For example, the [ConfigurationProperty] attribute decorating the DefaultProvider property tells ASP.NET to initialize DefaultProvider with the value of the <imageService> element's defaultProvider attribute, if present.
Listing 4. Class representing the <imageService> configuration section

using System;
using System.Configuration;

public class ImageServiceSection : ConfigurationSection

Next, you must register the <imageService> configuration section and designate ImageServiceSection as the handler for it. The Web.config file in Listing 5, which is identical to the one in Listing 3 except for the changes highlighted in bold, makes <imageService> a valid configuration section and maps it to ImageServiceSection. It assumes that ImageServiceSection lives in an assembly named CustomSections. If you use a different assembly name, you'll need to modify the <section> element's type attribute accordingly.

Listing 5. Making <imageService> a valid configuration section and registering ImageServiceSections as the configuration section handler

<configuration >
  <configSections>
    <sectionGroup name="system.web">
      <section name="imageService"
        type="ImageServiceSection, CustomSections"
        allowDefinition="MachineToApplication"
        restartOnExternalChanges="true" />
    </sectionGroup>
  </configSections>
  <connectionStrings>
    <add name="ImageServiceConnectionString" connectionString="..." />
  </connectionStrings>
  <system.web>
    <imageService defaultProvider="SqlImageProvider">
      <providers>
        <add name="SqlImageProvider" type="SqlImageProvider"
          connectionStringName="ImageServiceConnectionString"/>
      </providers>
    </imageService>
  </system.web>
</configuration>

Loading and Initializing Custom Providers

The final piece of the puzzle is implementing the image service and writing code that loads and initializes the providers registered in <imageService>'s <providers> element.

Listing 6 contains the source code for a class named ImageService that provides a programmatic interface to the image service. Like ASP.NET's Membership class, which represents the membership service and contains static methods for performing membership-related tasks, ImageService represents the image service and contains static methods for loading and storing images. Those methods, RetrieveImage and SaveImage, call through to the provider methods of the same name.

Listing 6. ImageService class representing the image service

using System;
using System.Drawing;
using System.Configuration;
using System.Configuration.Provider;
using System.Web.Configuration;
using System.Web;

public class ImageService

Before delegating calls to a provider, ImageService.RetrieveImage and ImageService.SaveImage call a private helper method named LoadProviders to ensure that the providers registered in <imageService>'s <providers> element have been loaded and initialized. LoadProviders uses the .NET Framework's System.Web.Configuration.ProvidersHelper class to do the loading and initializing. One call to ProvidersHelper.InstantiateProviders loads and initializes the registered image providers-that is, the providers exposed through the Providers property of the ImageServiceSection object that LoadProviders passes to ProvidersHelper.InstantiateProviders.

Observe that the ImageService class implements public properties named Provider and Providers that provide run-time access to the default image provider and to all registered image providers. Similar properties may be found in the Membership class and in other ASP.NET classes representing provider-based services.

Click here to continue on to part 9, Hands-on Custom Providers: The Contoso Times


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NETMVC框架(转)发布时间:2022-07-10
下一篇:
ASP.NETRepeater控件实现简单分页发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap