Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where in code should one keep data that doesn't change?

Tags:

c#

asp.net

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?

like image 704
shashi Avatar asked Jun 28 '12 07:06

shashi


2 Answers

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.

like image 173
David Hedlund Avatar answered Sep 26 '22 13:09

David Hedlund


I think you should use Cache instead of static variables.

I don't know any detail about your code, so if you are using a repository you could do something like that:

if [cache contains object or collection I need]
{
   return [object or collection from cache]
}
else
{
   [get object or collection from database]
   [save object or collection in cache]
   return [object or collection from database]
}

Then, if you change your cached entities you have to find a way to delete those cached entries to retrieve fresh data from the DB.

like image 31
Alessandro Avatar answered Sep 25 '22 13:09

Alessandro