ASP.NET 2.0: Custom Resource Provider Using Sql Database
The world of Web applications is quite different from Windows forms. The solutions that perform great for Windows applications are sometimes quite pain when developing solution designed for the web. ASP.NET 2.0 introduces new feature called declarative localization expressions. It allows you to easily localize web pages and by default it uses .resx files stored in App_LocalResources and App_GlobalResources folders in your web application. Visual studio 2005 supports easy resource creation for each web page by "Generate local resources..." menu entry. Localization to different language/culture is so simple as copying and renaming this automatically generated file and adding desired locale code to it's file name.
But there are also some drawbacks when using this in ASP.NET 2.0 web applications...
- The resource files (satelite assemblies) are locked all the time, the application is running (it's not easy to update there localized resources).
- It's difficult (if not impossible) to manage these resources directly from your own web application (for example let the user create new localization or edit existing ones on ther own).
But wait, we are in ASP.NET 2.0. Many thing can be extended and so this Resource provider can be. All you need is simply create your own ResourceProviderFactory based on ResourceProviderFactory class and modify web.config file. Now imagine, you could have your resources stored in Sql (or any other) database.
public sealed class SqlResourceProviderFactory : ResourceProviderFactory { public override IResourceProvider CreateGlobalResourceProvider(string classKey) { return new SqlResourceProvider(null, classKey); } public override IResourceProvider CreateLocalResourceProvider(string virtualPath) { return new SqlResourceProvider(virtualPath, null); } }
And coresponding declaration in web.config file...
<system.web> <globalization resourceProviderFactoryType="Core.Localization.SqlResourceProviderFactory"/> ...
This is all you need (except of course writing your own localization resource provider) to swap ASP.NET 2.0 core resource management functionality and suply your own implementation. The ResourceProviderFactory contains two methods...
- CreateGlobalResourceProvider - used for creating provider for global resources (accessed by GetGlobalResourceObject method or those declaratively used as <%$ Resources:Class, ResourceID %>)
- CreateLocalResourceProvider - used for local resources (accessed by GetLocalResourceObject method or those declaratively used as meta:resourcekey="Button1")
The IResourceProvider interface that is return by these two methods contains definition for following method and property.
object GetObject(string resourceKey, CultureInfo culture) IResourceReader ResourceReader
The GetObject method is used to get resource by resourceKey string and specific culture, where ResourceReader property returns all resources for specified virtualPath and current culture. I will not discuss any other details, instead i'm providing links to some custom database ResourceProviders implemented by other users...
I changed the GetResources function of the Helper class to interact with Oracle. When I simply write the query, Select Fields From Tablename, then it works fine. But when I changed this query into parameterized query, it doesn't work even CreateLocalResourceProvider function isn't execute.
I m also much astonished when I try to apply filter on dataview after getting all records from plan query (not parametrized)into a datable, the same problem occurs, even when I simply apply if statement before adding resouces into the Hashtable, same problem occurs.
But one thing which may help for some guru to answer me is when I open the Default.aspx page is desig n view, lable control showing error "Object reference not set to instance of an object". but it shows its value perfectly when I execute this site using plan query.
A lot of thanks in advance.