I am new to ASP.NET, experienced with WinForms and WPF. Go easy on me.
When my page loads, it hits the database and queries a table for choices to put in a drop down list. Now if you do a postback on the page, do you reload the values in the form_load? It seems unnecessary to hit the database twice. My assumption is that I put the form_load, I hit the database for the enums, then fill the drop down with the values I found.
I assume you’re talking about a “lookup table”, basically something that has a schema of {Id, Name} and is referenced as a foreign key from another table, correct?
This is an age-old question, and there’s no “right” answer.
If your enumeration is likely to change at all (perhaps it’s controlled by an administrative user), you won’t be able to use @Andrei Rinea’s suggestion, as you’ll need to refresh the value from the database. Ultimately, you’ll be best off doing as Keltex suggests and Cache the results with a short expiration — probably as little as 5 minutes. Just that small amount of caching could increase performance quite a bit, if you’re under a heavy load.
If your enumeration is unlikely to change… particularly if it’s not modifiable through the UI by an administrator, there’s a fun trick that I like to do, which is to map the enumeration to an actual C# enumeration, with the value of the
enumtied directly to the primary key of the enumeration table. In this way, you never have to hit the database at all to get a list of possible values.The drawback to this trick is that a new enumeration item requires a new compilation of your code and a new deployment. This may or may not be ideal, so use the trick with caution.