比ConfigurationSection,ConfigurationElement和ConfigurationElementCollection更简单/更好的东西?

时间:2013-07-10 10:20:32

标签: .net configuration web-config

这种配置方式非常棒,因为它允许您定义所需的内容,甚至可以为您的配置添加验证器。

然而,一旦你开始映射比带有属性或简单元素集合的元素更复杂的东西,这是一个很大的痛苦!

他们有没有提出更简单的东西,比如创建一个类/模型并将其直接映射到带有验证的所有配置部分,或者有人想出一个更简单的解决方案?

例如,为此我放弃了ConfigurationSection并使用xml和amp;来实现它。 xsd验证..但我对它不满意

实际上起初(在放弃ConfigurationSection之后)我试图将它实现为带有DataAnnotations的ViewModel,但是在调用validate方法而不是在控制器上下文中并且验证嵌套模型时会带来它自己的问题... < / p>

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

我喜欢使用像ConfigurationSection和DataAnnotation这样的东西进行验证,但对于上面的例子,它只是过于复杂。

任何人都能找到/知道更简单的东西,同时仍能验证模型的正确性吗?

由于

0 个答案:

没有答案