ASP.NET 2.0 Resources

Powered by Blogger

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...

7 Comments:

  • 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.

    By Blogger shakeel, at 1:26 PM  

  • I believe it is ethical to use the custom essay writing services. You do not still anything, you pay enough fair price for the service you use, you simply delegate the part of your work and can concentrate on what really matters to you. You are free to get an outside help from bestcustomessay.org/essays and delegate your writing assignments to professional writers same as if you would get your watches fixed or car repaired by third party experts.

    By Blogger ADmin, at 3:31 PM  

  • thanks for your sharing, I appreciate this. keep up the good work)
    essay writing service

    By Blogger Іван Пилат, at 6:25 PM  

  • The next magnificent document, almost like which he wrote. From either of the the things which I'd like on the net - ended up being typically the this aspect there isn't any disadvantage during measurements, for instance the the only real bed-sheet to a pieces of paper nor roughly a. A "freedom" is not going to sure your notions and also assist you to fit say all of your thought process but additionally just about every that are anxious for getting these products yourself.
    mobile casino

    By Blogger Nikolo Lloyd, at 7:10 PM  

  • an angler can take a fishing charter that splits its time between freshwater salmon fishing and deep-water halibut fishing. In May the combination trip would include king salmon, and in August it would include silver salmon. Discounted garcinia cambogia

    By Blogger steve7876, at 6:19 PM  

  • The next spectacular file, virtually similar to that this individual authored. From both on the what that I would like online - ended up being an this specific feature there isn't a negative aspect through sizes, for example your really the only bed-sheet with a waste papers or around the. The "freedom" Mobile wallet solution is not going to positive ones thoughts and in addition allow you to suit point out all your way of thinking but pretty much every which might be restless for getting the products by yourself.

    By Blogger steve7876, at 8:47 AM  

  • The next spectacular file, virtually similar to that this individual authored. From both on the what that I would like online - ended up being an this specific feature there isn't a negative aspect through sizes, for example your really the only bed-sheet with a waste papers or around the. The "freedom" Mobile wallet solution is not going to positive ones thoughts and in addition allow you to suit point out all your way of thinking but pretty much every which might be restless for getting the products by yourself.

    By Blogger steve7876, at 8:48 AM  

Post a Comment

<< Home

Created dolly