I am trying to make use of .NET configuration and understand custom sections, elements etc.
It seems that implementing these custom sections requires explicit declaration of getters and setters, generally resulting in code bloat.
For example here:
http://msdn.microsoft.com/en-us/library/2tw134k3.aspx
Specifically, it seems necessary for us to explicitly return and set things in the get and set methods.
// Create a "remoteOnly" attribute.
[ConfigurationProperty("remoteOnly", DefaultValue = "false", IsRequired = false)]
public Boolean RemoteOnly
{
get
{
return (Boolean)this["remoteOnly"];
}
set
{
this["remoteOnly"] = value;
}
}
With the following
[ConfigurationProperty("remoteOnly", DefaultValue = "false", IsRequired = false)]
public Boolean RemoteOnly { get; set }
not being equivalent to the above.
Is this really true – do we have to be verbose even with such vanilla properties?
Yes, because you’re relying on an external storage mechanism (the base class’s dictionary which ends up populating the config file).
Also, if you’re worried about this for code bloat, you are worrying about the wrong things. Write it once, never look at it again. You shouldn’t have so much code dealing with custom configuration settings that it will bloat your codebase. A teeny bump maybe, but not bloat. Also, I’d be happy you’re dealing with this now and not before VS2005 – there was a LOT more code you had to write (you had to parse the XML sections manually).
Finally, if you still hate it that much, you can always use “the last configuration section handler I’ll ever need”:
https://sites.google.com/site/craigandera/craigs-stuff/clr-workings/the-last-configuration-section-handler-i-ll-ever-need
It’s been a long time since he wrote that, but it should still work just fine.