views:

59

answers:

2

Let's say I have a configuration property that looks like this. Note that there is no default value.

[ConfigurationProperty("x", IsRequired = true)]
[StringValidator(MinLength = 1)]
public string X
{
    get { return (string)this["x"]; }
    set { this["x"] = value; }
}

Now I add my section like this:

<mySection x="123" />

I'll get this error:

The value for the property 'x' is not valid. The error is: The string must be at least 1 characters long.

It works if I change the configuration property to include a default like this:

[ConfigurationProperty("x", DefaultValue="abc", IsRequired = true)]
[StringValidator(MinLength = 1)]
public string X
{
    get { return (string)this["x"]; }
    set { this["x"] = value; }
}

This implies that validator validates the default value even if IsRequired is true. It also means that I have to include a dummy default values on all my properties to pass validation even though they won't actually be used.

Is this just bad design or is there a valid reason for this behavior?

+1  A: 

I have had this problem before. There was a valid reason for this but I cannot remember the details.

I cannot remember if this works but you can try declaring the property in the constructor where null is the default value.

public class CustomConfigurationSection : ConfigurationSection
{
    public CustomConfigurationSection()
    {
        public CustomConfigurationSection()
        {
            Properties.Add(new ConfigurationProperty(
                "x",
                typeof(string),
                null,
                null,
                new StringValidator(1), ConfigurationPropertyOptions.IsRequired));
        }
    }

    public string X
    {
        get { return (string)this["x"]; }
        set { this["x"] = value; }
    }
}

This is related to using default values and validators but is where a default value is wanted. http://msdn.microsoft.com/en-us/library/system.configuration.configurationproperty(VS.85).aspx#1

EDIT

I have just tried out the previous code and it does as I expected. My previous code did not compile as I missed out a constructor property so I have fixed that.

Bronumski
+1  A: 

As per my understanding, this behavior is highly required.

Since the configuration is one of the core area of any application, and suppose, no value has been provided for an application critical property, then the whole application may lead to some unwanted behavior(could be a crash, indefinite resource utilization etc). I think that is the reason, most of the .Net inbuilt configuration properties like Session timeout etc. were set to a default value and they will be applied even user didn't specified value.

Siva Gopal
I think you have missed the point of the question though. The OP is trying to create a configuration "required" configuration property and therefore does not need a default value. The configuration framework does not allow you to do this when using attributes to wire up the properties.
Bronumski