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


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



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?




DataAnnotation attributes not working in Asp.Net MVC2

1:



Book suggestion on this DDD concept specific to ASP.NET MVC and JSON? [closed]
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...


ASP.NET MVC 2 filling ViewModel data
You can use the following just as you would an enumeration....
JQuery function fails when content loaded from Ajax form
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).


How to check in ASP.NET MVC View if site is running on localhost or 127.0.0.1
All hard-coded values are stored in the type itself...


ASP.Net MVC (1) - how can I pass JSON to view but not as separate ajax call
of which only one will exist per AppDomain...


How do you globalise the ASP.Net forms authentication longinUrl for multiple languages?
.
Can i put ASP.NET in Js file?
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); }


2:


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


3:


Looking at your code, it looks like you're just trying to create a set of objects that really just put the enum PostVotePointValue into some sort of list.

I.e.

you already have what you need defined in just the enum itself.

I would encourage you to not define the same information in two places (this data store you are asking for and the enum).

This is common mistake I see people make.

They create a lookup table/list, then create an enum that mirrors the rows of the table and that means they have to modify two places for any change to the list.. If PostVotePointValue isn't an enum but just some constants or if there is more info you are planning on packing in, then this isn't relevant.

. Here's some examples of how to work with Enums as 'lists' from http://www.csharp-station.com/Tutorials/Lesson17.aspx.
   // iterate through Volume enum by name     public void ListEnumMembersByName()     {         Console.WriteLine("\n---------------------------- ");         Console.WriteLine("Volume Enum Members by Name:");         Console.WriteLine("----------------------------\n");          // get a list of member names from Volume enum,         // figure out the numeric value, and display         foreach (string volume in Enum.GetNames(typeof(Volume)))         {             Console.WriteLine("Volume Member: {0}\n Value: {1}",                 volume, (byte)Enum.Parse(typeof(Volume), volume));         }     }      // iterate through Volume enum by value     public void ListEnumMembersByValue()     {         Console.WriteLine("\n----------------------------- ");         Console.WriteLine("Volume Enum Members by Value:");         Console.WriteLine("-----------------------------\n");          // get all values (numeric values) from the Volume         // enum type, figure out member name, and display         foreach (byte val in Enum.GetValues(typeof(Volume)))         {             Console.WriteLine("Volume Value: {0}\n Member: {1}",                 val, Enum.GetName(typeof(Volume), val));         }     } } 
You should be able to adapt the above into an approach that will give you a list that you can use for databinding if you need it..


4:


"Never" is a very hard word indeed.. In your particular case you are asserting that not only is your PostVoteType data absolute and immutable, but so is the container collection.

Frankly I don't believe you can know that, because you are not the business (your interpretation of requirement is imperfect) and you are not psychic (your knowledge of the future is imperfect).

. I would suggest that you always store any data which cannot be expressed as an enumeration in some kind of repository.

Where you expect relational and/or transactional and/or mutable needs that means a database, if you expect high read to write ratio that can be a config file (which I believe this case should be).. Edit: In terms of memory persistance I agree with others that the cache is the best place to store this, or rather in a domain object which is backed by cache.. Aside: your construction of PostVoteTypes is horrible - strongly suggest you want a refactor :).


5:


If it doesn't change, is commonly accessed, and is the same for all users, then the .NET cache is the proper place.

Have a property that yields these values.

Inside, the property checks the cache for this list and returns the stored value; otherwise, it constructs it from scratch, adds to the cache, and returns the value.. This should still probably be configured in the database though, even if you cache it.

I imagine that you'll need to use these value in conjunction with other data in your DB..


6:


create a singleton class..


7:


When you need to often access the same data, and need not to store it into the underlying database, and that this data is about the same in every situation the application may encounter, then I suggest to use caching.

Caching is born from these requirements.

Caching is normally the fastest way to providing data as they are always kept in memory or something similar to ease and to make the access easier by the application.. Here is a nice caching tool provided with Microsoft Enterprise Library, the Caching Application Block.. I think it is worth to take the time to learn how to use it effectively..


8:


I am wondering why you could not just use a simple enum for this?.
public enum PostVoteType {     UpVote = 0,     DownVote = 1,     SelectAnswer = 2,     Favorite = 3,     Offensize = 4,     Spam = 5 } 



83 out of 100 based on 53 user ratings 953 reviews