none
Configuration Validator and Validate Against Another Element's Property Value RRS feed

  • Question

  • I found this a bit interesting.  I have (due to necessity) write a dll that manages configuration file settings.  I've done this before, so this is no big deal, but my current validation requirement is rather unique. 

    I'm using all custom configuration elements and those in question are two element collection lists.  To keep the discussion clear, let's say I have an element collection called List1 collection configuration elements called List1Item.  And there's a second called List2 collecting configuration elements called List2Item.  List1Items are used to define particular objects and their associated settings.  List2Items represent elements used to define which clients are using which List1Item.  To do this my List1Item has a "name" property.  List2Item has a "list1itemname" property.  I would like to use a validator for List2Items that verifies the string value entered exists in the List1 collection.  Is this possible?

    Because I am using a dll, I have created a ConfigInfo class used to load the configuration file data to simplify referring assembly usage.  I had thought about doing the validation in the Load() method of this class, but that seems to defeat the intentions of the Configuration Validators and would really go against any other Validator I may be using that is not called in the Load() method.  Any suggestions?


    Jason

    Monday, January 14, 2013 4:54 PM

Answers

  • If you have both collections defined under one configuration element or configuration section you can override the PostDeserialize method and have it check the elements of both collections and throw an exception if a problem is detected. http://msdn.microsoft.com/en-us/library/system.configuration.configurationelement.postdeserialize.aspx

    http://sherifelmetainy.blogspot.com/

    • Proposed as answer by Mike FengModerator Tuesday, January 15, 2013 9:12 AM
    • Unproposed as answer by HuffCAD Tuesday, January 15, 2013 1:20 PM
    • Marked as answer by HuffCAD Tuesday, January 15, 2013 3:04 PM
    Tuesday, January 15, 2013 12:31 AM
  • Ok... I believe I just had a duh moment.  Yes, overriding the PostDeserialize() is correct.  However, not for the element, but for the highest level ConfigurationSection class containing both collections.  This allows for a more comprehensive validation approach for those collections whose values should be depending upon another.  The sad thing is that you mentioned that, but I overlooked it for some reason.  Thanks for your help!

    Jason


    • Marked as answer by HuffCAD Tuesday, January 15, 2013 3:04 PM
    • Edited by HuffCAD Tuesday, January 15, 2013 3:05 PM
    Tuesday, January 15, 2013 3:04 PM

All replies

  • If you have both collections defined under one configuration element or configuration section you can override the PostDeserialize method and have it check the elements of both collections and throw an exception if a problem is detected. http://msdn.microsoft.com/en-us/library/system.configuration.configurationelement.postdeserialize.aspx

    http://sherifelmetainy.blogspot.com/

    • Proposed as answer by Mike FengModerator Tuesday, January 15, 2013 9:12 AM
    • Unproposed as answer by HuffCAD Tuesday, January 15, 2013 1:20 PM
    • Marked as answer by HuffCAD Tuesday, January 15, 2013 3:04 PM
    Tuesday, January 15, 2013 12:31 AM
  • I was actually wondering about that, but wouldn't this mean re-reading the config file?  If it has to do that with every List2Item, then wouldn't this consume additional resources unnecessarily and be slow?

    Jason

    Tuesday, January 15, 2013 12:44 PM
  • I tried using this.CurrentConfiguration in the PostDeserialize() override and then get my section group, then get my section.  When trying to get the section, I end up in this infinite loop as it attempts to read and re-read the same initial ConfigurationElementCollection.

    Jason

    Tuesday, January 15, 2013 2:18 PM
  • Ok... I believe I just had a duh moment.  Yes, overriding the PostDeserialize() is correct.  However, not for the element, but for the highest level ConfigurationSection class containing both collections.  This allows for a more comprehensive validation approach for those collections whose values should be depending upon another.  The sad thing is that you mentioned that, but I overlooked it for some reason.  Thanks for your help!

    Jason


    • Marked as answer by HuffCAD Tuesday, January 15, 2013 3:04 PM
    • Edited by HuffCAD Tuesday, January 15, 2013 3:05 PM
    Tuesday, January 15, 2013 3:04 PM