I'm implementing a somewhat complex configuration section for a system monitoring project:
<monitoring>
<components>
<component name="Remote Server"
statusProviderType="ClientEndpoint"
statusProviderName="endpoint1" />
</components>
</montioring>
<system.serviceModel>
<client>
<endpoint name="endpoint1"/>
<!-- Config removed for brevity -->
</client>
</system.serviceModel>
As in the example above, these configuration elements can refer to other configuration elements. In this case, a client endpoint which must implement a pre-determined contract which is part of the monitoring project.
The built-in configuration handling deals with any obvious syntax errors and simple errors where values are outside of a valid range. The problem is where configuration is the source of more complex errors:
- Referencing an element which does not exist.
- Referencing a Type which does not meet certain conditions.
Obviously, I wish to pick up on these errors, but where should this validation be done in the application?
I've considered these options so far, but I'm not sure any of them is correct:
- The configuration element validates itself and throws exceptions when it is read. Keeps everything at a configuration level, but introduces problems when trying to "build" a configuration section to write it to a configuration file.
- The component which consumes the configuration validates the configuration values when they are passed to it and throws exceptions based on the information received. Allows for more flexibility with configuration elements, but makes it harder to identify where the problem's origin is.
- The component which consumes the configuration validates the configuration as it is needed by its operation and throws exceptions when configuration is invalid. This nicely separates the behaviour from the configuration, but makes it less obvious when the configuration is the source of the problem.
Additionally, which exceptions are appropriate to throw for the sort of errors I am potentially handling? Bonus points for good reasoning with your answers.
I'm feeling rather clueless with all the options available, but knowing how hard configuration can be to debug with some systems, I really want a sensible solution.