Database Resource Entries
It may be helpful to review the database table structure to store the actual resource entries first. The example contains a SQL script to create a database named Customresourceproviderssample and a table named Stringresources. Table 1 contains the following fields:
Table 1. database table with Resource entry
Field |
Description |
ResourceType |
The category of each resource. You can use it to differentiate between local resources for different pages, or to differentiate global resource types based on user-defined names. |
Culturecode |
The culture code in the supported CultureInfo code used by. NET is based on the ISO standard. You can also extend the code for any missing code. |
ResourceKey |
The resource key to use to retrieve the resource. |
Resourcevalue |
The resource value. This table supports strings up to 4 K. |
In this example, all resources are stored in a separate table, although they can be distributed in several tables in order to optimize typical usage patterns in more complex or larger environments. The primary key for this table is a key combination, including ResourceType, Culturecode, and ResourceKey. A single resource value typically uses a primary key request. Figure 5 shows a partial view of the contents of the table.
Figure 5. Partial view of this sample resource entry
The ResourceType of a page resource is the name of the page, including its relative path in the application (that is, expressions.aspx,subdir1/expressions.aspx). This Convention distinguishes pages with the same name in different subdirectories, similar to the way the default resource provider model recognizes different local resource assemblies by subdirectories. The resource key for a control property follows the same naming convention as a typical page resource, using the control prefix and the property name, as follows.
[Prefix].[PropertyName]
Global resources have user-defined resourcetype. The sample code has several global resource categories: Glossary, Commonterms, and Config. The resource keys in this example are visually named for their contents.
The data access layer StringResourcesDALC the work to retrieve resources from the table based on the usage pattern of the provider model.
Extended ResourceProviderFactory
The ResourceProviderFactory type is the center of resource access in ASP.net 2.0, which is responsible for returning global or local resource providers based on the requested resource type. ResourceProviderFactory is an abstract base type that requires implementations of the following two methods: Createlocalresourceprovider () and Createglobalresourceprovider (). To create a custom provider factory, you need to inherit this base type to provide implementations of these methods. Both methods must return an instance of the resource provider that implements the IResourceProvider interface.
The basic resourceproviderfactory type declaration is shown in Listing 1.
Code Listing 1. ResourceProviderFactory abstract Type
public abstract class ResourceProviderFactory
{
protected ResourceProviderFactory();
public abstract IResourceProvider CreateGlobalResourceProvider(string classKey);
public abstract IResourceProvider CreateLocalResourceProvider(string virtualPath);
}