Resource files are useful in Web apps

Source: Internet
Author: User
Jeff Key
January 16,200 2

Visual Studio. NET makes it very easy to use resource files, so why not take advantage of them? You can store just about anything in them, including strings, images and persisted objects, but the most useful for web apps is strings. I was originally using them for error message management, but realized that they cocould more, especially in a text-intensive application where having packaged units might be beneficial. another advantage is their read-only nature; people (ie. clients) can't modify the contents unless you provide them with an application that writes satellite assemblies.

Availability
Resource files (. resx ,. resource) can be compiled into an assembly or put into their own assembly as a satellite resource. when compiled into an app, you're stuck with what you have at compile time, but that's not a problem if you're simply wrapping up something that won't change between builds. international Satellite assemblies require special consideration, but the support for them in the runtime is very cool and invaluable if the need arises.

Using in Web apps
The. NET framework gives us a number of tools that help us manage string resources, especially caching and the new Session and Application classes. these work wonderfully when the backing provider is a database. the strings can either be cached at load time if the application knows the string IDs/keys and can iterate through them, or on every request (check the cache, if it's not there, load value from the database, then cache ). this works well in most, if not all, instances. A feature lacking, though, is easy rollback. compiling resources into satellite assemblies affords a physical history, as well as incredibly flexible portability.

Creating a resource file
In my specific case, I found that my error messages were long enough to make my code look messy and decreased the readability. simply putting the error messages into a local resource file (compiled with the assembly) made the code much easier to follow.

You can add a resource file via the "Add Item... "Item in the Solution Explorer. when you open the file, you're presented with. net's XML editor, as the resx format is a simple XML document. the columns presented are name, value, comment, type and mimetype. when using strings, you only need to be concerned with the name and value columns.

Reading from the Resource
As with everything. net, there are a number of different ways to get at the resources. the ResourceManager class offers the simplest way. the most useful constructor takes an assembly and the root name to pick the resources from. for example, if you have a resource in your application ("myapplication") with the name of "res. resx ", you wowould create an instance with the following:

ResourceManager rm = new ResourceManager ("MyApplication. res", Assembly. GetExecutingAssembly ())

Once you have a reference to the ResourceManager, it's just a matter of calling its getstring method.

String s = rm. GetString ("TheKey ");

This simple feature, in conjunction with the String class's static Format method, makes for powerful string handling. Instead:

Throw new Exception ("The file '" + filename + "'Could not be deleted from the'" + dir + "'Directory because of the following error:" + ex. message + ". please ensure that file is not currently being used. ")

I can put the following string (name = "FileDeleteError) into the resource file:

The file '{0}' cannot be deleted from the '{1}' directory because of the following error: {2}. Please ensure that file is not currently being used.

And reference it with the following:

Throw new Exception (String. Format (rm. GetString ("FileDeleteError", filename, dir, ex. Message )));

Much nicer.

Dynamically loading and using satellite resource assemblies
Creating and using satellite resources is surprisingly easy. The simplest route is to create an assembly containing only resx files.

  1. Create an assembly containing (but not limited to) resx files and compile. (I called it "SatelliteResource. dll" in this example .)
  2. Create a reference to the assembly in the project that will use it, or place in its bin folder. The latter is preferred if you will be dynamically placing the files during runtime.
  3. In the calling program, create an Assembly object and load it with the assembly containing the resource. Assembly ass = Assembly. Load ("SatelliteResource ");
  4. Create a resource manager, passing in the new assembly and the file you want to read. ResourceManager rm = new ResourceManager ("SatelliteResource. res", ass );
  5. Read from the resource. string s = rm. GetString ("TheKey ");

Summary
While using resources in every app (especially web apps) may not be necessary, it is surely something that is useful when the need arises. the advanced capabilities not covered here, including serialized objects and images, can be used more widely in traditional Windows Forms applications and services.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.