Specifying the IConfigurationSource used for WPF integration

Topics: Validation Application Block
Apr 26, 2010 at 4:22 AM

In the EntLib 5.0 validation block, when using the ValidatorRule provided by the Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WPF library, I would like to be able to specify an implementation of IConfigurationSource to use. When not integrating directly with WPF we are able to do this in code as follows:

string configFilePath = @"ClientValidation.xml";
var configurationSource = new FileConfigurationSource(configFilePath);

var configurationValidatorFactory = ConfigurationValidatorFactory.FromConfigurationSource(configurationSource);
var validator = configurationValidatorFactory.CreateValidator<T>();
var validationResults = validator.Validate(obj);

But how do we specify that same instance of IConfigurationSource when using the WPF ValidatorRule?

I've tried something like this - in our bootstrapper:

UnityContainerConfigurator unityContainerConfigurator = new UnityContainerConfigurator(OurUnityContainer);
EnterpriseLibraryContainer.ConfigureContainer(unityContainerConfigurator, configurationSource);

But that configurationSource doesn't seem to be used by the WPF validator. And in looking at the EntLib 5.0 source, it seems that the WPF ValidatorRule is pulling the rules/rulesets out of app.config - by using the .NET ConfigurationManager. But I don't see any way of altering the IConfigurationSource??? 

Ultimately, I want to be able to specify the file from which to pull validation configuration - as the code above does - when using the WPF ValidatorRule. 

Thanks for your input.

 

Apr 26, 2010 at 4:34 AM

Try setting the master entlib container.

EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configurationSource)

Then everything in entlib will pull from that container.

 

Apr 26, 2010 at 4:08 PM

Thanks ctavares. I ended up finding the built-in option of specifying an externally-sourced file for sections of EntLib configuration. So it seems easier than I originally thought - which is great!

Here's the config section I ended up with in my app.config file:

 

 

  <enterpriseLibrary.ConfigurationSource selectedSource="System Configuration Source">
    <sources>
      <add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      <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="ClientValidation.xml" />
    </sources>
    <redirectSections>
      <add sourceName="File-based Configuration Source" name="validation" />
    </redirectSections>
  </enterpriseLibrary.ConfigurationSource>