C# 在web.config中存储一系列值-要使用的数据结构
以下场景中使用的最佳数据结构是什么 我想要一个基于某些物品价格的收费百分比。 例如if(最简单的场景,这里可能有更多的条目。)C# 在web.config中存储一系列值-要使用的数据结构,c#,asp.net,asp.net-mvc-3,C#,Asp.net,Asp.net Mvc 3,以下场景中使用的最佳数据结构是什么 我想要一个基于某些物品价格的收费百分比。 例如if(最简单的场景,这里可能有更多的条目。) 价格->百分比 20% $5-$10 -> 15% $10-$20 -> 13.5% >$20 -> 12.5% 我希望这是灵活的,所以我想把它放在web.config中(或者,如果您认为这是一个更好的主意-在sql server中) 如何实现这一点?我将把它放在SQL Server的表上;该表将有4列: 身份证 起始值 终值 百分比 如果有必要,我会在应用程序
价格->百分比
20%
$5-$10 -> 15%
$10-$20 -> 13.5%
>$20 -> 12.5%
我希望这是灵活的,所以我想把它放在web.config中(或者,如果您认为这是一个更好的主意-在sql server中)
如何实现这一点?我将把它放在SQL Server的表上;该表将有4列:
- 身份证
- 起始值
- 终值
- 百分比
如果有必要,我会在应用程序端捕获这些数据。您可以使用对象来确保数据库上的任何更新都能快速反映在应用程序端。我不会使用Web.config,因为Web.config主要适用于键值对,我不认为这里是这种情况。您可以使用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>
我最终使用了基于sql的东西,有两列,更低的价格和费用。但这也是一个很好的解决方案,正如您提到的自定义配置对象。
<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();