Store a range of values in web.config - what data structure to use

后端 未结 2 1624
渐次进展
渐次进展 2021-01-07 01:33

What is the best data structure to use for the following scenario?

I want to have a fee percentage based on the price of certain items. For example if (simplest scen

相关标签:
2条回答
  • 2021-01-07 01:59

    I would put it on table on SQL Server; the table would have 4 columns:

    • Id
    • StartValue
    • EndValue
    • Percentage

    I would catch this data on the application side if necessary. You can use a SqlCacheDependency object to ensure that any update on the database gets quickly reflected on the application side. I would not put on Web.config since Web.config is mostly suitable for Key-Value pairs and I don't see this being the case here.

    0 讨论(0)
  • 2021-01-07 02:02

    You can use ConfigurationSections (http://msdn.microsoft.com/en-us/library/2tw134k3.aspx)

    Basically, it let you serialize and deserialize from your web.config complex structures directly to a C# class you define. This is an example, it may not work perfectly (or even compile!) but it gives you the idea of what you can get from ConfigurationSection. Hope it helps.

    namespace Project
    {
        public class PricesConfiguration : System.Configuration.ConfigurationSection
        {
            public static PricesConfiguration GetConfig()
            {
                 return (PricesConfiguration )System.Configuration.ConfigurationManager.GetSection("pricesConfiguration") ?? new ShiConfiguration();
            }
    
            [System.Configuration.ConfigurationProperty("prices")]
            public PricesCollection Prices
            {
               get
               {
                  return (PricesCollection)this["prices"] ?? 
                    new PricesCollection();
             }
          }
       }
    
       public class PricesCollection : System.Configuration.ConfigurationElementCollection
       {
          public PriceElement this[int index]
          {
             get
             {
                return base.BaseGet(index) as PriceElement;
             }
             set
             {
                if (base.BaseGet(index) != null)
                   base.BaseRemoveAt(index);
                this.BaseAdd(index, value);
             }
          }
    
          protected override System.Configuration.ConfigurationElement CreateNewElement()
          {
             return new PriceElement();
          }
    
          protected override object GetElementKey(System.Configuration.ConfigurationElement element)
          {
             var price = (PriceElement)element;
             return string.Format("{0}-{1}->{2}%",price.Start,price.End,price.Percentage);
          }
        }
    
        public class PriceElement : System.Configuration.ConfigurationElement
        {
            [System.Configuration.ConfigurationProperty("start", IsRequired = false)]
            public int? Start
            {
                get
                {
                    return this["start"] as int?;
                 }
            }
    
            [System.Configuration.ConfigurationProperty("end", IsRequired = false)]
            public int? End
            {
                get
                {
                    return this["end"] as int?;
                }
            }
    
            [System.Configuration.ConfigurationProperty("percentage", IsRequired = true)]
            public string Percentage
            {
                get
                { 
                    return this["percentage"] as string;
                }
            }
        }
    }
    

    And the web.config will look like:

    <configuration>
      <configSections>
        <section name="pricesConfig" type="Project.PricesConfig, Project"/>
      </configSections>
    
      <pricesConfig>
        <prices>
            <add end="5" percentage="20" />
            <add start="5" end="10" percentage="15" />
            <add start="10" end="20" percentage="13.5" />
            <add start="20" percentage="12.5" />
        </prices>
      </pricesConfig>
    </configuration>
    

    for using it, just call

    var config = PricesConfiguration.GetConfig();
    
    0 讨论(0)
提交回复
热议问题