Resource Application Block 4.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 4.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, including string
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 4.1 - October 2008\Bin:

The Resource Application Block 4.1 consists of the following assemblies:

What Next?

Changes since v3.1

There are very few new features in v4.1 as the changes are nearly all internal. But as changes go, this was quite a massive exercise.

Acknowledgements

El Bruno did a lovely article on the Resource Application Block v3.1 that inspired and encouraged me to continue keeping it up to date. Besides, I am now using the RAB in all of my own developments so it makes sense to keep it in line with progress on the Enterprise Library.