Resource Application Block 3.1

The Resource Application Block is all about resource management, localization and globalization. The Resource Application Block gives developers a provider model to get to resources from a variety of different sources and source types using the features of the Enterprise Library 3.1. All configuration of those resources naturally uses the Enterprise Library Configuration Console.

The developer is not limited to the providers out-of-the-box but can extend the block by adding providers of their own in the normal Enterprise Library extensible fashion.

Main Features

The following are the main features associated with the Resource Application Block:

Key Uses

If you need convincing as to why you would use this block, have a look at the following reasons:

A Quick Code Sample

The following code sample shows the basic function of the block, how to get the default resource manager and use its resources. Note that an indexer is used, based on a string key and an optional type (string is the default) to get at the resources. The standard GetString(), GetObject() and GetStream() methods are still available however:

[C#]
// get the default resource manager. Note: this is an EntLibContrib.Resource.ResourceManager NOT System.Resources.ResourceManager
ResourceManager rm = ResourceFactory.GetResourceManager();
// get a string resource
string myString = rm["StringKey"];
// get an integer resource, this works for any type of resource
int myInt = (int)rm["IntegerKey", typeof(int)];
// get an audio resource, you would normally need to use the GetStream() method for this type of resource.
UnManagedMemoryStream myAudio = (UnManagedMemoryStream)rm["AudioKey", typeof(UnManagedMemoryStream)];


This next code sample shows how to get a named resource manager instance, set a specific culture and also how do do string formatting using the optional params parameter of the indexer:

[C#]
// get the "named instance" resource manager, this is the name you have given the configured resource manager in the console.
ResourceManager rm = ResourceFactory.GetResourceManager("named instance");
// set the culture to generic french.
rm.CultureInfo = new CultureInfo("fr");
// get a formatted string resource and format the string
string friendlyResponse = "bonjour";
string myFormattedString = rm["FriendlyMessageKey", friendlyResponse];


If the resource value is something like "mon ami dit {0}" then myFormattedString will contain "mon ami dit bonjour". Note: you can have more than one replaceable format string parameter in the same fashion as String.Format().

Installing the Assemblies

When you build the Resource Application Block the assemblies are compiled without a strong name key. If you want to put these assemblies into the GAC then you will need to assign a strong name key to each assembly and recompile (this is not neccessary for any design.dll assembly). If you use the BuildLibraryAndCopyAssemblies.bat console script then the assemblies are copied to the EntLibContrib bin folder. You can reference the assemblies from here for your application however; to use the Configuration console you will need to copy all of the following assemblies to your installation of the Enterprise Library Configuration Console which is usually in C:\Program Files\Microsoft Enterprise Library 3.1 - May 2007\Bin:

The Resource Application Block 3.1 consists of the following assemblies:

What Next?

Acknowledgements

I did not get much feedback from the Resource Application Block versions 1.1 and 2.0 so I very nearly did not bother with version 3.1 until I read .NET Internationalization by Guy Smith-Ferrier so my thanks to him for the inspiration and some neat ideas.

I know it might sound like a cliche but I must include my wife Margaret too. I have three young boys and you don't get to write this kind of stuff whilst carrying out a full program of fatherly responsibilities... Off to read Rupert Bear to the 6 year old, I will try not to localize him or turn Pong-Ping into a Frenchman.