Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to store static data in C# that will never change

Tags:

c#

asp.net-mvc

I have a class that stores data in asp.net c# application that never changes. I really don't want to put this data in the database - I would like it to stay in the application. Here is my way to store data in the application:

public class PostVoteTypeFunctions
{
    private List<PostVoteType> postVotes = new List<PostVoteType>();
    public PostVoteTypeFunctions()
    {
        PostVoteType upvote = new PostVoteType();
        upvote.ID = 0;
        upvote.Name = "UpVote";
        upvote.PointValue = PostVotePointValue.UpVote;
        postVotes.Add(upvote);

        PostVoteType downvote = new PostVoteType();
        downvote.ID = 1;
        downvote.Name = "DownVote";
        downvote.PointValue = PostVotePointValue.DownVote;
        postVotes.Add(downvote);

        PostVoteType selectanswer = new PostVoteType();
        selectanswer.ID = 2;
        selectanswer.Name = "SelectAnswer";
        selectanswer.PointValue = PostVotePointValue.SelectAnswer;
        postVotes.Add(selectanswer);

        PostVoteType favorite = new PostVoteType();
        favorite.ID = 3;
        favorite.Name = "Favorite";
        favorite.PointValue = PostVotePointValue.Favorite;
        postVotes.Add(favorite);

        PostVoteType offensive = new PostVoteType();
        offensive.ID = 4;
        offensive.Name = "Offensive";
        offensive.PointValue = PostVotePointValue.Offensive;
        postVotes.Add(offensive);

        PostVoteType spam = new PostVoteType();
        spam.ID = 0;
        spam.Name = "Spam";
        spam.PointValue = PostVotePointValue.Spam;
        postVotes.Add(spam);
    }
}

When the constructor is called the code above is ran. I have some functions that can query the data above too. But is this the best way to store information in asp.net? if not what would you recommend?

like image 895
Luke101 Avatar asked May 25 '10 18:05

Luke101


People also ask

How are static variables stored in C?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).

What is static data in C?

A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.

Are static variables stored on heap in C?

static variable stored in data segment or code segment as mentioned before. You can be sure that it will not be allocated on stack or heap.

When should static be used in C?

Static is a keyword used in C programming language. It can be used with both variables and functions, i.e., we can declare a static variable and static function as well. An ordinary variable is limited to the scope in which it is defined, while the scope of the static variable is throughout the program.


2 Answers

This is a candidate for an immutable struct that "looks like" an enumeration: (Also, I noticed you used the same id value for two of them, so I fixed that... You can use the following just as you would an enumeration...

PostVoteTypeFunctions myVar = PostVoteTypeFunctions.UpVote;

and real nice thing is that this approach requires no instance storage other than a 4-byte integer (which will be stored on stack, since it's a struct). All hard-coded values are stored in the type itself... of which only one will exist per AppDomain...

public struct PostVoteTypeFunctions 
{ 
    private int id;
    private bool isDef;
    private PostVoteTypeFunctions ( )  { } // private to prevent direct instantiation
    private PostVoteTypeFunctions(int value) { id=value; isDef = true; }

    public bool HasValue { get { return isDef; } }
    public bool isNull{ get { return !isDef; } }
    public string Name 
    { 
       get 
       {  return 
             id==1? "UpVote":
             id==2? "DownVote":
             id==3? "SelectAnswer":
             id==4? "Favorite":
             id==5? "Offensive":
             id==6? "Spam": "UnSpecified";
       }
    }
    public int PointValue 
    { 
       get 
       {  return // Why not hard code these values here as well  ?
             id==1? PostVotePointValue.UpVote:
             id==2? PostVotePointValue.DownVote
             id==3? PostVotePointValue.SelectAnswer:
             id==4? PostVotePointValue.Favorite:
             id==5? PostVotePointValue.Offensive:
             id==6? PostVotePointValue.Spam: 
                    0;
       }
    }
    // Here Add additional property values as property getters 
    // with appropriate hardcoded return values using above pattern

    // following region is the static factories that create your instances,
    //  .. in a way such that using them appears like using an enumeration
    public static PostVoteTypeFunctions UpVote = new PostVoteTypeFunctions(1);
    public static PostVoteTypeFunctions DownVote= new PostVoteTypeFunctions(2);
    public static PostVoteTypeFunctions SelectAnswer= new PostVoteTypeFunctions(3);
    public static PostVoteTypeFunctions Favorite= new PostVoteTypeFunctions(4);
    public static PostVoteTypeFunctions Offensive= new PostVoteTypeFunctions(5);
    public static PostVoteTypeFunctions Spam= new PostVoteTypeFunctions(0);       
} 
like image 162
Charles Bretana Avatar answered Sep 24 '22 16:09

Charles Bretana


It is difficult to tell from the fragment of code you have posted whether you expose any of the data outside the class.

If not, then this would work. However, if not, there are several issues:

  • If you are exposing the List, you should only ever return a copy of it as an IEnumerable<PostVoteType> using the yield keyword.
  • Make sure your PostVoteType is immutable, otherwise the references can be changed and the fields used might be altered
like image 32
Oded Avatar answered Sep 26 '22 16:09

Oded