Best practice for loading and using dictionary items?

Say I have 10 “categories” that I need to use in a web application. I currently store these categories in the database and get them (category name and identifier) ​​during pageload on my base page, storing them in a hash table and then using a hash table to refer to the categories. I realized that a DB call is made during each pageload. I only need to pull out these categories once, as they never change during a given session. What is the best way to do this?

  • Do what I do, but save it as an application variable?

  • Is it hard to encode categories in code and delete a database?

  • Store categories in session variable? And to call the database only if the session is empty?

  • Or something else?

I'm just curious to know what is the best practice for such things.

+4
source share
2 answers

If the categories are user dependent, I would save them in the Session variable; otherwise, I would use the ASP.NET caching function (or preferred distributed caching). This avoids getting into the database, while at the same time having the ability to control how long the categories should be cached.

+2
source

Calling a database is not always as expensive as it seems, a typical website makes dozens of database calls on every page. But if this becomes problematic in terms of performance, consider using an ORM solution. The excellent open source NHibernate comes to mind, which is the de facto standard for mapping databases to classes and objects. In addition to displaying, it automatically provides two levels of caching and pooling. Without any problems, your site surpasses any other landslide.

Lack of use of ORM? For many people, this is considered a pretty steep learning curve. If you want to read this, be sure to visit NHibernate Best Practices . Hard to read at first, but definitely worth it.

If you combine NHibernate with FluentNHibernate , it will become a breeze.

0
source

All Articles