views:

85

answers:

3

I have a class in my application that translates path tokens into fully qualified paths. For example: it can take a string like "%MYAPPDATA%" and return C:\Users\user.DOMAIN\AppData\Raoming\MyApp.

Alternatively, the class has an overload to the function that can take an enum instead of a string. For example: it can take the enum AppPaths.MyAppData and return C:\Users\user.DOMAIN\AppData\Raoming\MyApp.

I need to store the "lookup table" somewhere, but I'm not sure what the best method or structure is. Should I use a dataset and write the table to disk? Or just keep in it memory?

A single path value can map to a string and an enum. I suppose I can just keep an array in memory whose index maps to the integer value of the enum and do a search through the array when I'm passed a string.

Thoughts?

+2  A: 

Unless it is particularly large, I would just go with the array in memory. For a bit more functionality - and a bit more overhead - you could use a list or a dictionary. For lots of functionality - but lots of overhead - you could use an in-memory dataset.

Again, unless you have more than, say, 250-500 items, it really wouldn't make sense to use disk storage during the operation of your program: the overhead in both latency (retrieval time) and coding is just not worth it.

You may well, of course, keep records on disk as your long-term storage (e.g. to be loaded when the program starts). However, it looks from your problem that you may not even need this if you are pulling the data from the OS.

Mark Brittingham
I should just build the array when the object is constructed then?
vg1890
Yep - that is the approach I would take. If you don't want to write the code to look up matches, then investigate the List<YourClass> type or, better yet, the generic version of Dictionary. You'll still need a function to do compares but they are pretty easy.
Mark Brittingham
One more question - the translator is sort of a utility so I had it built as a static class that had a large case statement. Now that I need to keep the lookup table in memory, should I make the table shared state or make the class non-static?
vg1890
Wow - yes you should absolutely make the class non-static.
Mark Brittingham
+1  A: 

It all depends on the performance you desire.

You might store the data in database tables (one for the "raw" data, two for the relationship between the keys and the enumeration) and look it up with simple SQL queries. This may not be the fastest way, though, depending on the underlying database and caching mechanism.

You may also use two in-memory maps (dictionaries?) for faster lookup at runtime, intializing them at startup.

Manrico Corazzi
A: 

I would keep it in memory if it is impossible that it can grow to big - otherwise I'd store it in a database/file for later use.

Of course it depends on your usage. As far as I see you want to store the path to user-specific-data. Could it be that you've got a User-Class where you could store the path of the data?

if(Directory.Exists(User.Current.AppDataPath))

So you could probably get rid of the "lookup table" since every user knows where his AppData is - just a thought...

Gambrinus