Click here to Skip to main content
Click here to Skip to main content

Custom Resource Reader

, 1 Jun 2006
Rate this:
Please Sign up or sign in to vote.
Implementing custom resource reader with database
Sample Image - Custom_Resource_Reader.jpg

Introduction

This article would help you to create a custom resource reader if you are starting out. Resources are an application-building feature that allow you to place culture-specific items inside satellite files, rather than directly in your main application. When you build your application, you can identify aspects that are culture-specific, and make a different resource file for each culture where you think your application may be used. At run time, the appropriate set of resources will be loaded, based on the user's culture settings. The specific setting used is the CurrentUICulture for your thread, which the user can set programmatically.

When you make your application, it is housed in an 'assembly'. An assembly consists of the following:

  • The assembly metadata, also called the assembly manifest
  • Type metadata
  • The Microsoft Intermediate Language (MSIL) code that implements the types
  • A set of resources

You may also put these resource files in an external repository like a database. For that, you have to create a custom resource reader for your application.

Using the Code

For this article, language specific resource elements are placed in the Microsoft SQL Server database. Create a SQL Server database with a single table Messages. Its create script would look like this:

CREATE TABLE [dbo].[Messages]
(
[Id] [int] IDENTITY(1,1) NOT NULL,
[Key] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Default] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[de] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[mn] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_Messages] PRIMARY KEY CLUSTERED ([Id] ASC)WITH _
	(IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

There are three steps to creating a custom resource reader. The first step is to create a class that implements IResourceReader, which requires a Close method and an implementation for GetEnumerator.

public class DBResourceReader:IResourceReader
{
  //Database Connection
  private string ConnectionString;

  //Language to retrieve
  private string Language;
  
  //Constructor
  public DBResourceReader(string ConnectionString, CultureInfo Culture)
  {
   this.ConnectionString = ConnectionString;
   this.Language = Culture.Name; 
  }
 
  //To get enumerator to iterate through the resources hashtable
  public IDictionaryEnumerator GetEnumerator() 
  { 
   //Hashtable to store Key-values pairs for resources
   Hashtable htLanguage = new Hashtable();
   //SQL DB connection to database
   SqlConnection conn = new SqlConnection(ConnectionString); 
   //Command to select specific language key value
   SqlCommand command = conn.CreateCommand(); 
   if (Language == "") 
      Language = "Default";
      command.CommandText = "Select [key], [" + Language + "] from Messages";
   try 
    { 
    conn.Open();
    SqlDataReader reader = command.ExecuteReader(); 
    while (reader.Read()) 
     {
     //Add all the key value pairs for specific language to a hashtable
     if (reader.GetValue(1) != System.DBNull.Value)
         htLanguage.Add(reader.GetString(0), reader.GetString(1)); 
     }
    reader.Close();
    }
    catch {}
    finally
    {
     conn.Close(); 
    } 
  return htLanguage.GetEnumerator();
 }
 
 //You may close db connection here.
 public void Close()
 { }
 
 //dispose resources that are not required
 public void Dispose()
 { }

//Implement IEnumerable interface 
IEnumerator IEnumerable.GetEnumerator()
 {
  return this.GetEnumerator(); 
 } 
} 

The next step is to generate a custom resource set. It stores all the resources localized for one particular culture. To do this, create a derived class of DBResourceSet, and override GetDefaultReader:

public class DBResourceSet:ResourceSet
    {
       public DBResourceSet(string ConnectionString, CultureInfo Culture): 
		base(new DBResourceReader(ConnectionString,Culture))
        {}
       //Return custom reader as default one for reading language resources 
       public override Type GetDefaultReader()
        {
            return typeof(DBResourceReader);
        }
   } 

The next step is to generate a custom resource manager that uses the custom reader. To do this, create a derived class of ResourceManager, and override InternalGetResourceSet to allow for the creation of the resource reader defined earlier. The following code example demonstrates a simple ResourceManager:

public class DBResourceManager : ResourceManager
    {
        //Connection string for database
        private string ConnectionString;
        //Constructor
        public DBResourceManager(string ConnectionString)
        {
            this.ConnectionString = ConnectionString;
            //initialize ResourceSets hashtable 
            ResourceSets = new Hashtable(); 
        }
        //ResourceManager.GetString() makes use of this function
        protected override ResourceSet InternalGetResourceSet(
          CultureInfo culture, bool createIfNotExists, bool tryParents)
        {
            DBResourceSet rs = null;
            if (ResourceSets.Contains(culture.Name))
            {
                rs = (DBResourceSet)ResourceSets[culture.Name];
            }
            else
            {
                rs = new DBResourceSet(ConnectionString, culture);
                ResourceSets.Add(culture.Name, rs);
            }
            return rs;
        }
    }

Conclusion

This article explains how to create a custom resource reader by using SQL Server database as resource repository for storing localized resources. You can also implement resource reader interface by using a different resource repository.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Palwinder Singh
Team Leader
India India
Techinical Lead on Microsoft technologies.Also,worked with PHP/ MYSQL. My core expertise is on dot net framework 2.0,3.5 and 4.0.

Comments and Discussions

 
QuestionFor other resources like images I want to read from resx...Is it possible? PinmemberSuyash Kunte15-Jul-10 2:10 
QuestionHow can I use this example in a ASP.NET Project? Pinmemberasbscout19-Oct-06 4:59 
AnswerRe: How can I use this example in a ASP.NET Project? PinmemberTomaž Štih13-May-13 20:38 
GeneralCool!!! PinmemberMicrosoftBob1-Jun-06 6:58 
GeneralRe: Cool!!! PinmemberPalwinder Singh13-Jul-08 23:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 1 Jun 2006
Article Copyright 2006 by Palwinder Singh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid