Property Validation with External Configuration File

Topics: Validation Application Block
Mar 18, 2008 at 10:01 PM
Edited Mar 18, 2008 at 10:03 PM
I have started putting validation configurations in separate config files for each of my primary business objects. This works really well for keeping the files easily manageable and also allows easliy updating rule sets for one object without having any concern about the rule sets for other objects. I have also created some SelfValidation methods for some of my rule sets. I then create a new validator for the object using the CreateValidator(Type type, String ruleSet, IConfigurationSource configurationSource) method.

This works great when I want to validate the entire object. But I am now looking to integrate validation using the AspNet Integration to perform specific Property Validation through AJAX calls. However, it doesn't appear that there is an easy way to specify the Configuration Source to use when doing such validation. I trolled through the EntLib code, and it seems that the PropertyValidationFactory always uses the default configuration that is specified in the web.config file. Obviously, this scenario doesn't work for me since none of my validation configs are referenced in the web.config at all.

Does anyone have any suggestions about how I can accomplish property-specific validation using a specific Configuration Source?

Thanks in advance for any help.
Mar 20, 2008 at 3:02 AM
The way I'm doing it right now which is not great but at least it works is by modifying the App.Config at runtime:

public bool UpdateConfigurationSource(string strselectedSourceValue)
{
XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

if (xmlDoc != null)
{
XmlNode ConfigurationSourceNode = xmlDoc.SelectSingleNode("configuration/enterpriseLibrary.ConfigurationSource");

if (ConfigurationSourceNode != null)
{
ConfigurationSourceNode.Attributes"selectedSource".Value = strselectedSourceValue;

xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

return true;
}

else
return false;
}
}

Ok so basically in the Load Event of the form or User Control call that function and pass the right Configuration Source.
Mar 28, 2008 at 7:33 PM
Thanks for the reply. I may have to go that route. I've also considered customizing the PropertyValidatonFactory to allow me to send in the location of the config file to use.
Apr 19, 2011 at 12:09 PM

You can always try a static way by setting an external configuration source section in the App.config file :

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <enterpriseLibrary.ConfigurationSource selectedSource="File-based Configuration Source">
    <sources>      
      <add name="File-based Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
           filePath="ValidationsRules.config" />
    </sources>
  </enterpriseLibrary.ConfigurationSource>  
</configuration>