2004年3月1日

resource files are useful in web apps


Jeff Key
January 16, 2002

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 could offer 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 VS.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 would 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 of:

throw new Exception("The file ‘" + filename + "’ cannot be deleted from the ‘" + dir + "’ directory because of the following error: " + ex.Message + ". Please ensure that 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 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.

posted @ 2004-03-01 20:37 coollzh 阅读(417) 评论(0) 编辑

Database Connection String

http://www.connectionstrings.com/index.asp

posted @ 2004-03-01 09:47 coollzh 阅读(350) 评论(0) 编辑

asp.netPRO Readers Choice Awards 2004

asp.netPRO Readers Choice Awards 2004

Add-in
C# Refactory
Xtreme Simplicity
 
www.xtreme-simplicity.net
Chant Speechkit
Chant
 
www.chant.net
CodeRush for Visual Studio .NET
Developer Express
 
www.devexpress.com
CodeSMART 2003 for VS.NET
AxTools
 
www.axtools.com/
DeKlarit
ARTech
 
www.deklarit.com
DevPartner Studio Professional Edition
Compuware
 
www.compuware.com/products/devpartner/studio.htm
IntraWeb
Atozed Computer Software
 
www.atozed.com/intraWeb/index.iwp
Total .NET SourceBook
FMS
 
www.fmsinc.com/dotnet/SourceBook
Visual Perl
Active State
 
www.activestate.com
None of the above.  
Charting & Graphics Tool
.netCHARTING
WebAvail Productions & Corporate Web Solutions
 
www.dotnetcharting.com
Chart FX for .NET
Software FX
 
www.softwarefx.com
ComponentOne WebChart for ASP.NET
ComponentOne
 
www.componentone.com/products.aspx?ProductCode=1&ProductID=54
DotImage WebForms
Atalasoft
 
www.atalasoft.com/components/dotimage/WebForms.aspx
Dundas Chart for .NET
Dundas Software
 
www.dundas.com
Measurement Studio
National Instruments
 
www.ni.com
Nevron Chart for .NET
Nevron
 
www.nevron.com
Organization Chart .Net
AASoftech
 
www.aasoftech.com
None of the above.  
Chat Application
Absolute Live Support .NET
Xigla Software
 
www.xigla.net
Active Data Online WebChat
Active Data Online
 
www.activedataonline.com.au
Adventia Chat Server .NET
Adventia
 
www.adventia.com
ChatSpace
Akiva
 
www.chatspace.com
None of the above.  
Code Editor
ASP Express
ASP Express
 
aspexpress.com
HomeSite
Macromedia
 
www.macromedia.com/software/homesite
Multi-Edit
Multi Edit Software
 
www.multiedit.com
PrimalScript Scripting IDE
Sapien
 
www.sapien.com
Textpad
Helios Software Solutions
 
www.textpad.com
None of the above.

posted @ 2004-03-01 09:13 coollzh 阅读(680) 评论(0) 编辑