这种配置方式很棒,因为它允许您定义所需的内容,甚至可以将验证器添加到您的配置中。
但是,一旦您开始映射比具有属性的元素或简单元素的集合更复杂的任何东西,这将是一个很大的痛苦!
他们不是想出了一些更简单的东西,比如创建一个类/模型并将其直接映射到一个带有验证和所有功能的配置部分,还是有人想出了一个更简单的解决方案?
例如,为此我放弃了 ConfigurationSection 并使用 xml 和 xsd 验证实现了它..但我对此并不满意
实际上,起初(在放弃 ConfigurationSection 之后)我试图将它实现为带有 DataAnnotations 的 ViewModel ,但是当调用 validate 方法而不是在控制器上下文中并验证嵌套模型时,这带来了它自己的问题......
[XmlRoot("Configuration", Namespace = "urn:Configuration")]
public class Configuration
{
[XmlElement("Common")]
public Common Common { get; set; }
[XmlElement("FaceBook")]
public FaceBook FaceBook { get; set; }
[XmlArray("Domains")]
public List<Domain> Domains { get; set; }
[XmlArray("Links")]
public List<Link> Links { get; set; }
}
public class Common
{
[XmlAttribute("analyticsId")]
public string AnalyticsId { get; set; }
[XmlAttribute("analyticsDomain")]
public string AnalyticsDomain { get; set; }
[XmlAttribute("segmentationId")]
public int SegmentationId { get; set; }
[XmlAttribute("serviceId")]
public int ServiceId { get; set; }
}
public class FaceBook
{
[XmlAttribute("apiKey")]
public string ApiKey { get; set; }
[XmlAttribute("appId")]
public long AppId { get; set; }
[XmlAttribute("canvas")]
public string Canvas { get; set; }
}
public class Replacement
{
[XmlAttribute("key")]
public string Key { get; set; }
[XmlAttribute("value")]
public string Value { get; set; }
}
public class Link
{
[XmlAttribute("key")]
public string Key { get; set; }
[XmlAttribute("value")]
public string Value { get; set; }
}
public class Domain
{
[XmlAttribute("host")]
public string Host { get; set; }
[XmlAttribute("culture")]
public string Culture { get; set; }
[XmlAttribute("paymentRequired")]
public bool PaymentRequired { get; set; }
[XmlAttribute("paymentDocumentId")]
public int PaymentDocumentId { get; set; }
[XmlAttribute("paymentTimeout")]
public int PaymentTimeout { get; set; }
[XmlAttribute("default")]
public bool Default { get; set; }
[XmlArray("Replacements")]
public List<Replacement> Replacements { get; set; }
[XmlElement("AgeRange")]
public List<AgeRange> AgeRanges { get; set; }
}
public class AgeRange
{
[XmlAttribute("ageMin")]
public int AgeMin { get; set; }
[XmlAttribute("ageMax")]
public int AgeMax { get; set; }
[XmlAttribute("botPercentage")]
public int BotPercentage { get; set; }
[XmlAttribute("genderPercentage")]
public int GenderPercentage { get; set; }
[XmlAttribute("anonymousDomain")]
public string AnonymousDomain { get; set; }
[XmlAttribute("certifiedDomain")]
public string CertifiedDomain { get; set; }
[XmlElement("Salon")]
public List<Salon> Salons { get; set; }
}
public class Salon
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("default")]
public bool Default { get; set; }
[XmlAttribute("optGroup")]
public string OptGroup { get; set; }
}
我很想使用带有 DataAnnotation 的 ConfigurationSection 之类的东西进行验证,但是对于上面的示例,它只是过于复杂了。
任何人都发现/知道一些更简单的东西,同时仍然能够验证模型的正确性?
谢谢