问题描述
什么是用于以下情形的最佳数据结构?
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 scenario, could have way more entries here.)
Price -> Percentage
<$5 -> 20%
$5-$10 -> 15%
$10-$20 -> 13.5%
>$20 -> 12.5%
和我想这是灵活的,所以我希望把这个在web.config中(或者,如果你认为它是个更好的主意 - 在SQL Server)
And i want this to be flexible and so i want to put this in the web.config (or if you think its a better idea - in sql server)
如何去实现呢?
推荐答案
您可以使用ConfigurationSections(http://msdn.microsoft.com/en-us/library/2tw134k3.aspx)
You can use ConfigurationSections (http://msdn.microsoft.com/en-us/library/2tw134k3.aspx)
基本上,它让你序列化,并从你的web.config复杂结构直接反序列化到你定义了一个C#类。这是一个例子,它可能无法很好地工作(甚至是编译!),但它给你,你可以自ConfigurationSection得到什么的想法。希望它帮助。
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;
}
}
}
}
和在web.config看起来像:
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>
有关使用它,只需调用
var config = PricesConfiguration.GetConfig();
这篇关于存储某个范围的web.config中值 - 用什么数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!