-
Notifications
You must be signed in to change notification settings - Fork 201
Add Exists
to IConfigurationSection
#292
Comments
Thanks @poke. What would the application do differently if the value of a specific key had been intentionally set to null as opposed to not set at all? It is perfectly fine for the application to model required configuration settings on top of Configuration (e.g. by throwing exceptions if In fact, our configuration sources are not required to be able to represent a null value (in practice, very few can), so an empty string is generally a better representation of a value intentionally left "blank". Also, I think checking that |
Thanks for the quick reply!
That’s a very good question; I guess the answer is that there should be no difference. The reason I am asking for this is mostly because I want to short-cut logic when a whole configuration tree (/section) does not exist. In my example, I wanted to make the “Credentials” section support different kinds of credentials. So I could first check whether the section exists at all (if not, skip the whole logic), otherwise check whether specific combinations of values exist. And if the section is misconfigured, then I could throw an error that the configuration is wrong; it was specified but is wrong, so the application shouldn’t default to “no credentials”. So while there is no difference between non-existent and setting it to null, there is a difference between specified with children, and not specified or null. So maybe, I’m not asking for an After all, when I set a value for a section, there are two cases: Setting a value directly (which impacts the |
I don't think this statement is true: "Having children invalidates the Value property immediately, because a section cannot have both; since the children collection is the actual value." |
@HaoK Yes, that is correct: A section can have a value and children at the same time. Anyway, I can see how having an I will move this to the backlog and track user feedback. We can add it if enough customers want it or if we get a good PR. |
I definitely don't like this approach with
Later, I use
With suggested solution this will not work. |
Right now, when you get a configuration section using
GetSection()
there is no simple way to figure out whether that section actually exists. The only thing you could do is check if the value is non-null or if the count ofGetChildren()
is larger than zero:And this doesn’t even work if the section was actually set with a value of
null
on purpose.I would like to see some
Exists
method/property that tells me whether the queried section exists in the configuration. Otherwise, it’s difficult to detect optional configuration parts.As an example, I want a configuration section
Credentials
where credentials can be specified. If they are not specified, then that’s fine too. So the configuration may look like this:Or the section may be completely missing. So I get the configuration section using
GetSection
and now, I want to detect whether that section exists in the configuration or not.The text was updated successfully, but these errors were encountered: