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:
  • Greater flexibility to choose how you host your resources. With the Resource Application Block you can now choose whether to centralise or distribute resources, whether to leave resources in editable files rather than embed into assemblies, pull in resources from other projects and thus treat resources like reusable modules as per the Object Oriented paradigm,
  • You can use either the stand-alone Enterprise Library Configuration Console or the Visual Studio integrated Console to manage all Resource Application Block settings.
  • You have a choice of resource providers to manage resources from a variety of sources such as assembly embedded resources, xml resources (.resx files), binary resources (.resources files) and database resources (any DB you can get to from the Data Access Application Block which with EntLib v4.1 combined with EntLib Contrib v4.1 is now 8 different DB types).
  • If you need to manage resources from a source type not currently supported then you can extend the application block by creating custom resource providers
  • If you need to manage resources using a manager not currently supported then you can extend the application block by creating custom resource managers
  • You can generate strongly typed resource classes for any resource provider type directly from the Enterprise Library Configuration Console in either C# or VB.
  • A web resource provider is included to enable the developer to use the Resource Application Block with the localization features provided within ASP.NET. This works for both local and global resources.
  • All Resource Application Block managers are based on an extended ComponentResourceManager to enable reflection-style resource assignments to be made to Windows Forms component properties using the ApplyResources method within the ExtendedComponentResourceManager. Unfortunately, owing to the current closed nature of the Windows Forms designer CodeDomProvider model, with respect to localization, it has not been possible to extend the full Windows Forms localization feature, however see Windows Forms Designer for what you can do with the ExtendedComponentResourceManager.
  • A resource provider presents the developer with an appropriate resource manager type. Each resource manager supported within the Resource Application Block is provided with a corresponding resource writer to enable editing of the various resource storage types using any IResourceWriter enabled resource editor. These resource writers also support the extended data node interface allowing for comments, file references and custom types to be stored (except for binary resource files that do not support comments).
  • Management and monitoring features include Windows Active Directory Group Policy integration and instrumentation features such as Performance Counters, Windows EventLog logging and WMI events.
  • Dependency Injection support. The Resource Application Block now integrates with Unity v1.2 for the creation of all its objects.

Key Uses

If you need convincing as to why you would use this block, have a look at the following reasons:
  • You simply need to localize your application. The Resource Application Block gives you way more choices for managing localization than with the standard framework.
  • You have a large number of resources spread between many files. The standard approach is to hold a set of resources for each assembly of an application. If you have a large number of assemblies in your application and you are supporting several different cultures then the number of resource files can be considerable, managing these files and managing translators and translations etc becomes a complex operation. You can use the Resource Application Block to centralise those resources or at least break them down into a manageable set. The database provider is an ideal choice when things start to get out of hand.
  • You think you might need to manage some of your resources on-the-fly without having to re-compile and re-install assemblies each time. The Resource Application Block can provide resources directly from source files (e.g. .resx or .resources files) and does not hold those files open once read. This allows for on-the-fly changes to resource values e.g. spelling corrections, word replacement, change of colour etc.
  • You may need to change from assembly embedded resources to a database as an application scales up. Because all resource storage types are managed through configuration it makes it very easy to swap from one storage type to another.

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:
  • EntLibContrib.Resource.dll - the main assembly for the block
  • EntLibContrib.Resource.Database.dll - to comply with the rule that all application blocks must be able to work independantly of any other block, the data resource manager and provider, which has a dependancy on the Data Access Application Block was separated out to its own assembly.
  • EntLibContrib.Resource.Configuration.Design.dll - the plug-in interface to the Enterprise Library Configuration Console for the Resource Application Block.
  • EntLibContrib.Resource.Database.Configuration.Design.dll - the plug-in interface to the Enterprise Library Configuration Console for the Resource Application Block data provider.
  • EntLibContrib.Common.dll - Common Core code shared between all EntLibContrib extensions
  • EntLibContrib.Common.Configuration.Design.dll - Common Configuration Console design code, shared between all EntLibContrib extensions

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.
  • Integration of the Unity Container did my head in as the only real documentation was the source code, however I soldiered on and this version now uses Unity v1.2 as its DI container. All the code was rigourously vetted and refactored where necessary to improve performance, reduce dependencies and implement object and resource caching where sensible.
  • The old EntLibContrib.Resource.Store.dll has gone. This was a resource only assembly for all resources used by the RAB itself. So it was a bit incestuous using itself for its own messages. This is all fine and dandy until something goes wrong before the RAB has really got going and then finds it needs the RAB to get at an exception message! Such bootstrap messages, as I called them, had to be held separately, embedded in the EntLibContrib.Resource.dll assembly, so it no longer made sense to use the RAB for itself and now all the RAB's own messages are held internally.
  • The RAB also contained a number of innovative UIType Editors like the FilteredFileNameEditor and the ResourceSelectorEditor which might be useful elsewhere as I discovered when I wrote the Query Application Block (QAB). These editors have now been moved to the EntLibContrib.Common.Configuration.Design.dll assembly.
  • Extensibility has increased as it was only possible to create custom resource providers before. Now you can also create custom resource managers.

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.

Last edited Dec 16, 2009 at 3:47 PM by ewdev, version 7

Comments

pcsece411 Jan 24, 2013 at 5:07 AM 
Hi,

I tried to use the sample code but its giving an error related to type casting.

So i did like this

ResourceManager rm = (ResourceManager)ResourceFactory.GetResourceManager();

Its builded successfully.

While running , I am getting the exception like"The type initializer for 'EntLibContrib.Resource.ResourceFactory' threw an exception."

Could you please help me to resolve the issue...

tsinghua Sep 14, 2011 at 8:21 AM 
well, firstly glad to see this block.

However, did someone check the sample code before post it? The first line is incorrect:

ResourceManager rm = ResourceFactory.GetResourceManager();

What GetResourceManager function return is an interface.