Where to store constant objects that will be used in my application

前端 未结 5 1966
[愿得一人]
[愿得一人] 2021-01-23 05:00

Lets say I have an item, which has fields(properties)

  1. Location
  2. Average value
  3. Usability

And I have 10-15 items

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-23 05:54

    I’d use web.config and store this in app settings area and then create one class that will read these retrieve it as a list.

    Here is how it could look in web config and C# code.

           
        
        
        
        
        
        
        
     
    
    public class SomeClass
    {
    private string location;
    private double avgValue;
    private int usability;
    
    public string Location 
    {
        get { return location; }
        set { location = value; }
    }
    public double AvgValue
    {
        get { return avgValue; }
        set { avgValue = value; }
    }
    public int Usability
    {
        get { return usability; }
        set { usability = value; }
    }
    }
    
    public class Config
    {
    public static List Items
    {
        get
        {
            List result = new List();
    
            for (int i = 1; i <= Convert.ToInt32(WebConfigurationManager.AppSettings["count"]); i++)
            {
                SomeClass sClass = new SomeClass();
                sClass.AvgValue = Convert.ToDouble(WebConfigurationManager.AppSettings["avgValue_" + i.ToString()]);
                sClass.Location = WebConfigurationManager.AppSettings["location_" + i.ToString()];
                sClass.Usability = Convert.ToInt32(WebConfigurationManager.AppSettings["usability_" + i.ToString()]);
            }
    
            return result;
        }
    }
    

    }

提交回复
热议问题