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

I have a class that stores data in an asp.net C # application that never changes. I really do not want to put this data in the database - I would like it to remain 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 executed. I have some functions that may also request data above. But is this the best way to store information in asp.net? if not, what would you recommend?

+5
source share
8 answers

, " " : ( , , id , ... , ...

PostVoteTypeFunctions myVar = PostVoteTypeFunctions.UpVote;

, , 4- ( , ). ... 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);       
} 
+3

, , , - .

, . , , :

  • , IEnumerable<PostVoteType> yield.
  • , PostVoteType , ,
+1

"" - .

, PostVoteType , . , , , ( ), ( ).

, , - . , / / , , (, , ).

: , - , , , , .


, PostVoteTypes - :)

+1

, , , PostVotePointValue - . , . ( , , ). , , . / , , , , .

PostVotePointValue , , , .

, Enums "" 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));
        }
    }
}

, , , .

+1

, , .NET . , . ; , .

, , , . , .

0

, , , . . - , , , - .

, Microsoft Enterprise Library, Caching Application Block.

I think it's worth the time to learn how to use it effectively.

0
source

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
}
0
source

create a singleton class.

-1
source

All Articles