Jakew
Consulting, hacking, and motorcycles

Resources 3 ways

Thursday, 20 December 2007 08:05 by jakew

I'm building some server controls for a client. My controls are going to need to emit java script as part of their output. I've got a few choices available to do accomplish this:

  1. Just put the java script inline with my C#. (i.e. have a string build concatenate the strings together)
  2. Keep the java script in its own file on the server
  3. Embed the java script as a resource

I don't like the first option. If I need to change any of the code I have to deal with rebuilding the string. Plus I think it makes the code ugly and less maintainable. The second option is pretty good because we keep the java script separate, if I need to change it I just open the JS file and make the change. However this means that I have trust that the DLL with the server control and its related JS files stay together. Also somebody could come along and jack with my code in ways I didn’t really intend.

So embedding the JS file as a resource seems like a good compromise. I only have to deploy one file, the JS is kept separate and my code stays maintainable.

The only reason I'm bothering to write this is because I forgot how to deal with embedded resources. What I hate about that is it is a reoccurring pattern for me. But when I take the time to write something like this I tend to not forget.

The first two ways to deal with resources involves using RESX files. In visual studio, once you have created a project add a resource file to it. In the resource file add string1 and give it a value. How do you get that value back. There are two ways. The first way is to use a ResourceManager like so:

ResourceManager

The first like news up a ResourceManager instance by passing in the name of the resource we want to deal with. In this case I named my RESX file ResourceTable and the namespace for the project is ResourceApp - so "ResourceApp.ResourceTable". If you have trouble finding the right name use Reflector to find it.

The second way, and easier, is to use the designer that VS gives you free of charge. In this case it's just one line:

ResourceDesigner

To see what is going on expand the RESX file and look at the designer.cs file hidden underneath. Of course this feature is only available if you are using VisualStudio 2005 or newer.

The next way to deal with resources is to embedded them in the assembly. In this case I added a text file to my project. In the file's properties I set the build action to "Embedded Resource". To get at the contents of the file you have to use the Assembly object and pull out the resource stream. Like so:

ResourceStream

You could compress the code down to three lines, but that makes it less readable.

Using this approach you can embedded nearly anything you want and be able to access it at run time.

The normal use for this stuff is to deal with localization. Instead of hard coding your strings you put them in a resource file at set its locale. If you need to run in a new locale you just need to provide a new set of resources for it. But since I don't usually do that stuff I don't need to talk about it.

The project, such as it is, is available here.

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed