Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8160825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:21:49+00:00 2026-06-06T18:21:49+00:00

I have defined models based on the tables in the database. Now there are

  • 0

I have defined models based on the tables in the database. Now there are some models whose data hardly changes. For example, the categories of the products that an e-comm site sells, the cities in which it ships the products etc. These don’t change often and thus to avoid hitting the db, these are currently saved as static variables.

The question is where in code should these static variables be located. Currently, in ProductCategory class (which is also the model representation) a static List is defined which if empty calls the db and loads the Product Categories. Similarly, the City class has a similar static List and so on.

These static Lists are then used across the application. I was thinking of creating a class called StaticData and then keeping all the static Lists within this class. That is now instead of

ProductCategory.AllCategories.Find(p => p.Id = 2) 

I shall have

StaticData.AllProductCategories.Find(p => p.Id = 2) 

Which do you think is a better approach? I am also aiming for testability and decoupled code.

Also, is there a better way to achieve these? How do you do something similar in your code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:21:50+00:00Added an answer on June 6, 2026 at 6:21 pm

    If it’s static enough to be compiled into your code, and never has to change in runtime, you could use a static class, see for instance the common pattern of turning enums into classes.

    Something like that should probably work well in your scenario:

    public class Vehicle
    {
        public static Vehicle Car = new Vehicle("Car");
        public static Vehicle MotorBike = new Vehicle("MotorBike");
        public static Vehicle PeopleMover = new Vehicle("PeopleMover");
    
        private Vehicle(string name)
        {
            this.name = name
        }
        private string name;
    }
    

    Using an enum or a class with static members like that one should help you get rid of those nasty magic numbers in your code as well, but seeing as the syntax isn’t correct there (missing an =) I’ll assume that that’s just sample code and doesn’t look like anything in your real code base.

    If changes do sometimes occur, and you would want your list of categories in the database, they could be tightly cached (as in almost never expiring, but manually invalidated on an update). Depending on the size of the list of categories, I’d consider putting the entire collection in one big cache, and querying the cached object for individual categories, rather than keeping one separate cache entry for each.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model defined as below: class Example(models.Model): user = models.ForeignKey(User, null=True) other
I have a Model which has some constants defined, like below: class Order(models.Model): WAITING
I have two models, Category and Point . The associations are defined as: Category
In this situation I have two models, Comment and Score. The relationship is defined
All, i have the following model defined, class header(models.Model): title = models.CharField(max_length = 255)
I'm after a model-based database class for PHP/MySQL. But it must have one particular
I want to have the equivalent of two completely unrelated tables in my database,
I'm developing a CMS largely based on Zend Framework components. Some of the database
We have built a .net (asp) web based solution (SQL Server database) for a
I have a script which generates my PHP models dynamically based on the structure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.