views:

149

answers:

3

I've been reading others questions regarding storing settings in the web.config. Most of you agree that's a good way to store settings.

I just have another doubt about that.

In my app, I need to map some ID's in the web.config. For example: [Table = UserType] 1 - User /2 - Admin

Is it a good idea to store these settings in the web.config so I know what is the right ID in my application? Do you have a better solution?

Thanks guys, G

+1  A: 

If that values doesn't change too often, it's better to create a enum to store that values. An enum sample could be:

enum UserType
{
    User  = 1,
    Admin = 2
}

For more options, also take a look into [Flags] attribute: Enums, Flags, and C# — Oh my! (bad pun…)

Keep in mind every time you edit your web.config file, your IIS application pool get recycled.

Rubens Farias
I see.. so I should create a class to store these enums right? And then I create a enum like this:enum UserType ( User, 1; Admin, 2;)?
Guillermo Guerini
Yes, you just need to create a enum, not a class
Rubens Farias
I'll create a class to store all the Enums that I need! Better to keep it organized. :)
Guillermo Guerini
How can I get the int 1, 2, etc from the Enum? Because I need to compare the ID I receive from the db to this enum. What's the best way to do that?
Guillermo Guerini
A: 

I typically use the web.config to store things like connection strings, or key/value pairs that rarely [or never] change.

What you described would be ideal for an enum or perhaps a look up table in the database.

Jack Marchetti
A: 

In my web application I have a number of "Configuration" settings that exceed the structure of the Web.Config file, and don't want the web site to restart after changing them.

I ended up creating a number of xml config files where the xml data maps to objects that get cached into collections. This allows us to change the config on the fly, while not restarting the web site. We have a filewatcher that triggers a reload of the cache objects from the Xml files when something in the configuration directory gets modified.

You can also use the database for this obviously, but in our case this was configuration data that is maintained by development and deployed with the build.

Joel Provost