ResourceManagerProvider


You have learned from this chapter so far that the ResourceManager and ComponentResourceManager classes provided with the .NET Framework are not the only resource managers in the world. Maybe you will choose to use one of the resource managers in this chapter, or maybe you have been inspired to write one of your own. What is a certainty, however, is that there is too much potential in this idea to live with committing your project to a specific resource manager. Furthermore, everything changes, and what is certain is that new resource managers will be written either by Microsoft or by third parties, and you will want to throw away your old, worn-out yesteryear resource managers in favour of the latest widget. This leaves us with a new problem: How can we use resource managers where we do not commit ourselves to a given resource manager class?

This is the same problem that faces ADO.NET: How can you use ADO.NET classes without committing to a given data provider? ADO.NET solves this problem in ADO.NET 2 using DbProviderFactory classes, and we implement a similar concept here. For ASP.NET 2.0 applications, be sure to read the section "Using Custom Resource Managers in ASP.NET 2.0," later in this chapter.

The ResourceManagerProvider class has only static methods, a static field, and a static property:

 public class ResourceManagerProvider {     private static Type resourceManagerType =         typeof(ComponentResourceManager);     public static ComponentResourceManager         GetResourceManager(Type type)     {         return (ComponentResourceManager) Activator.CreateInstance(             resourceManagerType, new object[] {type});     }     public static ComponentResourceManager GetResourceManager(         string baseName, Assembly assembly)     {         return (ComponentResourceManager) Activator.CreateInstance(             resourceManagerType, new object[] {baseName, assembly});     }     public static ComponentResourceManager GetResourceManager(         string baseName, Assembly assembly, Type usingResourceSet)     {         return (ComponentResourceManager) Activator.CreateInstance(             resourceManagerType, new object[]             {baseName, assembly, usingResourceSet});     }     public static Type ResourceManagerType     {         get {return resourceManagerType;}         set         {             if (value.FullName ==                 "System.ComponentModel.ComponentResourceManager" ||                 value.IsSubclassOf(typeof(ComponentResourceManager)))                 resourceManagerType = value;             else                 throw new ApplicationException(                     "ResourceManagerType must be a sub class of "+                     "System.ComponentModel."+                     "ComponentResourceManager");         }     } } 


The public static ResourceManagerType property maps onto the private static resourceManagerType field and holds the Type used to create new resource managers. The private field defaults to the ComponentResourceManager Type, but the public property can be set like this:

 ResourceManagerProvider.ResourceManagerType =     typeof(DbResourceManager); 


You would make this assignment in the application's startup process.

The remaining static GeTResourceManager methods all create a new resource manager from the specific resource manager type and differ only in the parameters that they accept and pass on to the resource manager constructors.

So instead of writing this:

 ResourceManager resources =     new ResourceManager(typeof(CustomerBusinessObject)); 


you could write this:

 ResourceManager resources =     ResourceManagerProvider.GetResourceManager(     typeof(CustomerBusinessObject)); 


The benefit of this approach is that you can change an entire application's resource manager classes to a different class by changing a single line of code:

 ResourceManagerProvider.ResourceManagerType =     typeof(TranslationResourceManager); 


A translator, for example, could use the ResourcesResourceManager class during the translation process, and the live version of the application could use the ComponentResourceManager class.

If you like the idea of using the ResourceManagerProvider, you might wonder whether you have managed to track down all of the cases of creating a new resource manager object throughout your application and changed them to use Resource ManagerProvider, or whether you have let any fall through the net. If so, take a look at Chapter 13, "Testing Internationalization Using FxCop," and the "ResourceManager not provided by provider" rule that exists to find these rogue bits of code.




.NET Internationalization(c) The Developer's Guide to Building Global Windows and Web Applications
.NET Internationalization: The Developers Guide to Building Global Windows and Web Applications
ISBN: 0321341384
EAN: 2147483647
Year: 2006
Pages: 213

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net