【问题标题】:Store a range of values in web.config - what data structure to use在 web.config 中存储一系列值 - 使用什么数据结构
【发布时间】:2011-12-25 02:07:08
【问题描述】:

以下场景使用的最佳数据结构是什么?

我想根据某些商品的价格计算费用百分比。 例如如果(最简单的情况,这里可能有更多条目。)

Price -> Percentage
    <$5 -> 20%
    $5-$10 -> 15%
    $10-$20 -> 13.5%
    >$20 -> 12.5% 

我希望它灵活,所以我想把它放在 web.config 中(或者如果你认为它是一个更好的主意 - 在 sql server 中)

如何实现这个?

【问题讨论】:

    标签: c# asp.net asp.net-mvc-3


    【解决方案1】:

    您可以使用 ConfigurationSections (http://msdn.microsoft.com/en-us/library/2tw134k3.aspx)

    基本上,它允许您从 web.config 复杂结构直接序列化和反序列化到您定义的 C# 类。这是一个示例,它可能无法完美运行(甚至无法编译!),但它让您了解可以从 ConfigurationSection 获得什么。希望对您有所帮助。

    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;
                }
            }
        }
    }
    

    web.config 将如下所示:

    <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>
    

    要使用它,只需调用

    var config = PricesConfiguration.GetConfig();
    

    【讨论】:

    • 我最终使用了基于 sql 的东西,有 2 列,价格和费用较低。但这也是您使用自定义配置对象提到的一个很好的解决方案。
    【解决方案2】:

    我会把它放在 SQL Server 的表上;该表将有 4 列:

    • 身份证
    • 起始值
    • 结束值
    • 百分比

    如有必要,我会在应用程序端捕获这些数据。您可以使用SqlCacheDependency 对象来确保数据库上的任何更新都能快速反映在应用程序端。我不会使用 Web.config,因为 Web.config 最适合键值对,我认为这里不是这种情况。

    【讨论】:

      猜你喜欢
      • 2020-01-05
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 2018-10-28
      相关资源
      最近更新 更多