Load several ConfigurationSource

Topics: Building and extending application blocks, General discussion, Validation Application Block
Mar 15, 2010 at 10:01 AM

Hi,

I hope you can help me.
I use enterpriseLibrary.ConfigurationSource  for load several configuration files for validation application block (VAB).
This is a big project with many modules, so I want to create a specific configuration file for each module.

The problem is that enterpriseLibrary.ConfigurationSource  have selectedSource property, and I can't load all <sources> sections.

I know about configSource property, but I want some config source file, not one!

Do you have idea what to do?

Thanks!!

Mar 15, 2010 at 11:38 AM

Hi,

If this scenario is specific to the Validation App Block, I think you can use the overload of ( ValidationFactory.CreateValidator<Person>(new FileConfigurationSource("theFilePath")); ) which accepts a parameter Iconfiguration source, you can pass a instance of FileConfigurationSource which points to the config file that you want the VAB to use. Please see if this can be a solution for you.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 15, 2010 at 12:15 PM

First All, thanks for your response!

The solution proposed was not enough for me because I use UI integration, In UI Side I'm not creating the Validator myself but I use ValidationProvider.
On the service side, all modules use the same function, so I do not know what configuration file suitable for every validation.

Is it impossible to append some configuration files?

Thank you so much! 

Mar 15, 2010 at 12:50 PM

I'm not sure I understand your needs correctly, do I summarize correctly when I say you have multiple Validation Application Block configuration files and want to merge them to a single IConfigurationSource to enable validation on? It that's so, please take a look at this article, it explains how to do exactly this.

Mar 16, 2010 at 7:18 AM

Sorry for the long break ....
You understand me correctly!!!

What's still missing is how I load this IConfigurationSource into app.config settings as defualt selected source or as configSource of validation section.

I need it because In UI Side I'm not creating the Validator myself.

You help me a lot!!

Thanks!!

Mar 16, 2010 at 11:10 AM
Edited Mar 16, 2010 at 11:11 AM

The article explains how you can do this by calling ValidationFactory.CreateValidator method with the IConfigurationSource you created. When this is not feasable, because you don't want any custom code, you can also create an ConfigurationSourceElement. This allows you to configure it in the configuration. While I haven't tested it, the following might work.

Create a ConfigurationSourceElement for the ValidationConfigurationSourceCombiner, as follows:

 

public class ValidationConfigurationSourceCombinerElement : ConfigurationSourceElement
{
    public ValidationConfigurationSourceCombinerElement() 
        : this("File Configuration Source Combiner", string.Empty)
    {
    }

    public FileConfigurationSourceElement(string name, string filePaths)
        : base(name, typeof(ValidationConfigurationSourceCombiner))
    {
        this.FilePaths = filePaths;
    }

    public override IConfigurationSource CreateSource()
    {
        var sources =
            from filePath in filePaths.Split(';')
            where !string.IsNullOrEmpty(filePath)
            select new FileConfigurationSource(filePath);
    
        return new ValidationConfigurationSourceCombiner(sources);
    }

    [ConfigurationProperty("filePaths", IsRequired=true)]
    public string FilePaths
    {
        get { return (string) base["filePaths"]; }
        set { base["filePaths"] = value; }
   }
}

 

Now decorate the ValidationConfigurationSourceCombiner with the ConfigurationElementType attribute, so that it nows about the configuration element:

 

[ConfigurationElementType(typeof(ValidationConfigurationSourceCombinerElement))]
public class ValidationConfigurationSourceCombiner : IConfigurationSource
{
    // implementation
}

Now you should be able to configure the ValidationConfigurationSourceCombiner in your configuration:

 

 

<configuration>
  <configSections>
    <section name="enterpriseLibrary.ConfigurationSource"
	  type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, etc etc" />
  </configSections>
  <enterpriseLibrary.ConfigurationSource selectedSource="Configuration Source Combiner">
    <sources>
      <add name="Configuration Source Combiner"
        type="MyNamespace.ValidationConfigurationSourceCombiner, MyAssembly"
        filePaths="validation1.config;validation2.config;validation3.config" />
    </sources>
  </enterpriseLibrary.ConfigurationSource>
</configuration>

 

I hope this helps.

 

Mar 16, 2010 at 11:59 AM

It's a great solution!!!

It's work well and clean!

I think that it should be out of the box.

Many thanks! :-)

Sara.