Resource Managers
The Resource Application Block
Resource Manager type is actually a wrapper for any one of a set of Resource Managers that inherit from
ExtendedComponentResourceManager which in turn inherits from the
System.Resources.ResourceManager type. One example is the
BinaryResourceManager.
The key method within the System.Resources.ResourceManager type is the
InternalGetResourceSet() method which is responsible for obtaining a single set of resources for a single culture. It is also responsible for the fallback feature of resource management. For example, if you ask for resources for the culture
fr-CA but this set of resources does not contain values for all the resources but you have all the common French resources in the generic set
fr. Then resource managers will fetch resources from the
fr-CA set first, if they exist, otherwise fallback to fetching resources from the
fr set and so on until you reach the
neutral or
invariant culture.
The InternalGetResourceSet() method returns the set of resources as a
ResourceSet type. Not only does the ResourceSet contain a single set of resources but it is also responsible for marshalling the correct
IResourceReader and
IResourceWriter for reading and writing the set of resources from a resource store.
The ExtendedComponentResourceManager
Microsoft wrote the
ComponentResourceManager to work with their
CodeDomLocalizationProvider and
ResourceCodeDomSerializer to read and write localizable Windows Forms component properties to and from embedded assembly resources. The key feature of the ComponentResourceManager is the
ApplyResources() method that takes a component and a key name and uses reflection to match resources to component properties. The Resource Application Block resource managers rely on several of the available constructors of the
System.Resource.ResourceManager type, especially the one that takes a base name and an assembly. Although the ComponentResourceManager inherits from System.Resource.ResourceManager it only implements the constructor that takes a single
Type. This made it unsuitable as a parent for the resource managers for the Resource Application Block. For this reason the
ExtendedComponentResourceManager was written to implement all of the System.Resource.ResourceManager constructors and to provide the ApplyResources() method. Unfortunately, because it was not possible to inherit from the ComponentResourceManager this gives us big problems integrating with the Windows Forms designer for resource management, however not all is lost see
Windows Forms Designer for more details.
In addition to the ApplyResources() method, the ExtendedComponentResourceManager also integrates with the Enterprise Library instrumentation interface to provide performance, monitoring and logging information.
Assembly Based Resources
This is the default resource storage type used by the System.Resource.ResourceManager so nothing special is needed beyond the ExtendedComponentResourceManager.
File Based Resources
Both Binary and Xml resource managers use file based resources. A
FileResourceManager abstract type inherits from ExtendedComponentResourceManager and overrides the InternalGetResourceSet() method to handle the reading of resources from files. The
BinaryResourceManager and
XmlResourceManager both inherit from FileResourceManager to provide the appropriate
ResourceSet type to the FileResourceManager's InternalGetResourceSet() method.
Database Based Resources
The
DataResourceManager inherits from the ExtendedComponentResourceManager and overrides the InternalGetResourceSet() method to provide a
DataResourceSet type to the method. The DataResourceSet handles reading resources from a database.
ResourceSets
All Resource Application Block ResourceSets inherit from the
CommonResourceSet which in turn inherits from
System.Resources.ResourceSet. One example is the
BinaryResourceSet.
The key feature of a ResourceSet is to hold a table of resources for a single culture and, in addition, to associate a specific type of resource reader and resource writer with the resource set by providing the
GetDefaultResourceReader() and
GetDefaultResourceWriter() methods.
Note: the GetString(), and GetObject() methods also come from ResourceSet called by similar methods within System.Resource.ResourceManager.
The
CommonResourceSet adds a few additional features to the ResourceSet type such as a
Count property to return the number of resources in its table, a
Resources property to expose the inner table, a
CreateDefaultResourceReader() and a
CreateDefaultResourceWriter() to create an instance of an
IResourceReader and
IResourceWriter respectively. The methods included in the base type as described above only returned the type of default reader and writer. I have to give credit to Guy Smith-Ferrier for the inspiration on this one.
The Resource Application Block ResourceSets
There is no AssemblyResourceSet because this is the default type.
- BinaryResourceSet - this serves to provide a BinaryResourceReader and BinaryResourceWriter to the InternalGetResourceSet() method.
- XmlResourceSet - this serves to provide an XmlResourceReader and XmlResourceWriter to the InternalGetResourceSet() method.
- DataResourceSet - this serves to provide a DataResourceReader and DataResourceWriter to the InternalGetResourceSet() method.
The resource readers and resource writers use the Enterprise Library to get their configuration. In addition to reading and writing to and from the configured resource store, these custom readers and writers also implement the
IResourceDataNode interface for storing additional resource related data and custom types.
The IResourceDataNode Interface
The basic resource item in a set only carries a resource name key and a value. Using the
IResourceDataNode interface the resource item can carry additional information that includes a comment, a type name and a file reference.
- Comments* - These are simple strings associated with a resource item to give a human reader a description of the item for use with resource editors (*binary resources cannot support comments).
- Type Names - This is essential for resource stores that do not store resources as serialized CLR types. This enables a resource editor to reconstitute the resource as its proper type for display and editing. For example, a colour resource can be reconstituted as a Color object and be displayed as a shape of the appropriate colour.
- File References - A file reference is a pointer to an external file, typically a UNC name or Url. Xml resources use file references. An alternative to a file reference is to binary serialize the file contents into the resource store, this is the preferred option for binary and data resources.