Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 避免使用ConfigurationPropertyAttribute重复属性名称3次_C#_.net_Configuration_Code Cleanup_System.configuration - Fatal编程技术网

C# 避免使用ConfigurationPropertyAttribute重复属性名称3次

C# 避免使用ConfigurationPropertyAttribute重复属性名称3次,c#,.net,configuration,code-cleanup,system.configuration,C#,.net,Configuration,Code Cleanup,System.configuration,在代码中重复ConfigurationPropertyAttribute名称三次确实让我很烦恼。 很容易错过拼写错误或复制/粘贴属性,而忘记更新名称的一个实例 声明一个常量只能解决其中一个问题。有更好的办法吗 我试着反思,但列举属性似乎更麻烦,也更难看 [ConfigurationProperty("port", DefaultValue = (int)0, IsRequired = false)] [IntegerValidator(MinValue = 0, MaxValue = 8080,

在代码中重复
ConfigurationPropertyAttribute
名称三次确实让我很烦恼。
很容易错过拼写错误或复制/粘贴属性,而忘记更新名称的一个实例

声明一个常量只能解决其中一个问题。有更好的办法吗

我试着反思,但列举属性似乎更麻烦,也更难看

[ConfigurationProperty("port", DefaultValue = (int)0, IsRequired = false)]
[IntegerValidator(MinValue = 0, MaxValue = 8080, ExcludeRange = false)]
public int Port
{
    get
    {
        return (int)this["port"];
    }
    set
    {
        this["port"] = value;
    }
}

我知道DRY只是一个原则,在现实世界中,原则必须让位给实用主义。但我肯定有人有更干净的方法?

为什么使用常数不能解决所有这三个问题

例如:

class MyConfigurationElement : ConfigurationElement
{

    // Use public, private or internal, as you see fit.
    private const string PortName = "port";

    // Add something like this, if you also want to access the string value
    // and don't want to recompile dependend assemblies when changing the
    // string 'port' to something else.
    // public static readonly PortNameProperty = PortName;

    [ConfigurationProperty(PortName, DefaultValue = (int)0, IsRequired = false)]
    [IntegerValidator(MinValue = 0, MaxValue = 8080, ExcludeRange = false)] 
    public int Port  
    {
        get
        {
            return (int)this[PortName];
        }
        set
        {
           this[PortName] = value;
        }
    }    
}

旁注:如果您也可以考虑使用。坦率地说,我几年前才测试过它,从那以后就再也没有使用过它,但也许它也解决了您对DRY的担忧。

如果您愿意,可以对配置元素使用非声明性方法。互联网上到处都可以找到例子,特别是在同时展示这两种方法的地方

class MyConfigurationElement : ConfigurationElement
{
    private static ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();
    private static ConfigurationProperty _portProperty = new COnfigurationProperty("port", ..... ); // Will leave as example for you to add validator etc.

    static MyConfigurationElement()
    {
         _properties.Add(_portProperty);
    }

    protected override ConfigurationPropertyCollection Properties
    {
        get { return _properties; }
    }

    public int Port  
    {
        get
        {
            return (int)this[_portProperty];
        }
        set
        {
           this[_portProperty] = value;
        }
    }    
}

我认为在复制/粘贴/编辑错误的意义上。复制端口属性以生成Foo属性。您将属性声明从PortName更改为FooName,但忘记更改get/set引用,因此它可以很好地编译。解决所有问题的解决方案允许指定名称一次且仅一次,而不是重复三次。“但使用常量仍然比重复的魔法字符串有改进。”哈奇特同意。然而,我认为我们不应该把事情复杂化。一点小心和良好的测试很容易发现这样的问题。此外,仍然存在XSD文件的问题,无论如何,您都应该创建XSD文件(以便在编辑app.config文件时获得适当的intellisense支持)。在这里,您又有了(属性)名称。除了从单个源代码生成代码/XSD之外,这里没有什么可以帮助的。+1:我总是忽略这种方法;-)这还有一个好处,就是说它比声明方式更快(在运行时)(这就是为什么他们在WCF/System.ServiceModel配置中使用它的原因,该配置非常广泛-对不起,我记不起我在哪里读到的源代码)。可以说,这存在一个“忘记”向集合中实际添加/删除ConfigurationProperty的问题,但只在代码中提供get/set属性。好吧,我想无论采用哪种方法,最终你都必须知道自己在做什么,并采用适当的“工艺”。