Config puzzle

Topics: Enterprise Library Core, General discussion, Validation Application Block
May 30, 2007 at 4:02 AM
I am using Entlib 3.1 May 2007 release in winform application. My shell project consists of a single app.config file, which contains following config sections:

<configSections>
      <section name="securityConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Security.Configuration.SecuritySettings, Microsoft.Practices.EnterpriseLibrary.Security, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      <section name="compositeUI" type="Microsoft.Practices.CompositeUI.Configuration.SettingsSection, Microsoft.Practices.CompositeUI" allowExeDefinition="MachineToLocalUser" />
      <section name="netTiersService" type="XXXXXXX.Data.Bases.NetTiersServiceSection, XXXXXXX.Data" allowDefinition="MachineToApplication" restartOnExternalChanges="true" />
</configSections>

Now I want to use a Validation Block. So instead of adding "validation" section in app.config, I have created ValidationRules.config in my BusinessEntity project. Which looks something like this?

<configuration>
      <configSections>
            <section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      </configSections>
      <validation>
            <type assemblyName="XXXXXXX.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="XXXXXXX.Entities.YYYYY">
                  ......
            </type>
      </validation>	
</configuration>

Also all my validation rules are defined in above xml file. I am not using any validation attributes in my code.

Questions
  1. How should I tell Windows Form Validation Provider to load validation rules from ValidationRules.config?
  2. How can tell app.config to load ValidationRules.config from a different place and still load rest of configSections?
  3. If I define 'File Configuration Source' in app.config and add my ValidationRules.config as mentioned in some posts here and in QuickStart,
rest of configSections will not load as I changed the configuration source.

Can anyone point me out what I am doing wrong or what is the best way to handle this situation?

Thanks in advance
May 30, 2007 at 3:51 PM
Is specifying the configSource an option for you?

I saw this on Nikhil Kothari's Weblog @ http://www.nikhilk.net/ConfigSourceAttribute.aspx and have since used it for simplifying the management of our config files as the Ent Lib sections can tend to be verbose. (Nikhil rocks BTW! )
You still need to list this as a config section, but the body of the validation tag is specified in the file <validation configSource="ValidationRules.config" />
The contained 'chunk' in the referenced file needs to be fully formed, i.e. contain the full config section.

//What your definition might look like:
app.config file contains:
<configuration>
      <configSections>
            <section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      </configSections>
      <validation configSource="ValidationRules.config" />
</configuration>
ValidationRules.config file contains:
      <validation>
            <type assemblyName="XXXXXXX.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="XXXXXXX.Entities.YYYYY">
                  ......
            </type>
      </validation>
//Example of our setup
<configuration>
	<configSections>
		<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral" />
		<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral" />
		<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral" />
		<section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=2.0.0.0, Culture=neutral" />
		<section name="instrumentationConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.Configuration.InstrumentationConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral" />
	</configSections>
	
	<!-- Enterprise Library Sections -->
	<instrumentationConfiguration configSource=".\config\instrumentationConfiguration.config" />
	<loggingConfiguration configSource=".\config\loggingConfiguration.config" />
	<exceptionHandling configSource=".\config\exceptionHandling.config" />
	<dataConfiguration configSource=".\config\dataConfiguration.config" />
	<connectionStrings configSource=".\config\connectionStrings.config" />
	<cachingConfiguration configSource=".\config\cachingConfiguration.config" />
</configuration>

Hope this helps.

Regards,
George
May 30, 2007 at 4:12 PM
I will try my best here.

1) Unfortunately, the Winform Validation Provider will only look at the configuration source specified in your application's app.config file.
This is either the app.config itself or a separate file that you specify for getting all your entlib configuration settings. I just found out about
this when someone mentioned it using the PropertyProxyValidator in ASP.NET. See ASP.NET WorkItem:
http://www.codeplex.com/entlib/WorkItem/View.aspx?WorkItemId=10619. You may want to add the Winform Issue as a WorkItem, too.

2) You cannot specify different configuration files for different blocks in the configuration file. You can only specify them programmatically
in overloads, like: ValidationFactory.CreateValidator<Customer>(new FileConfigurationSource("CustomerRules.config")).

3) This is a limitation via configuration, but you can programmatically do it as mentioned in 2.

The bummer here is that we are pidgeon-holed a bit in where the validation rules are found using the ASP.NET and Winform Integration which I didn't
pick up on during the beta testing. I suspect we won't get a fix for this until next year unless we add them as workitems and enough people vote. The
P&P Teams have longer development cycles due to limited resources so we typically have to wait longer for fixes.

Regards,

Dave

______________________________

David Hayden
Microsoft MVP C#
May 30, 2007 at 4:46 PM
This seems like an issue similar to: http://www.codeplex.com/entlib/WorkItem/View.aspx?WorkItemId=10283

We've actually created an additional Validator for this scenario within the "Contrib community" www.codeplex.com/entlibcontrib, called "ExteranallyConfigurableObjectValidator".
It should be avaible under the "Source Code"-tab.

If this indeed is the same problem, let me know and i'll whip up a ExternallyConfigurablePropertyProxyValidator as well (unless I run into MAX_PATH issues ;-)).
This should be relatively easy -given the designtime and other fundation is already in place in the Contrib project.


Olaf
May 30, 2007 at 5:28 PM
GMariakis,

I hadn't noticed your post up above until just now. I didn't realize you could specify a configSource to specify a particular section is in a different file. That is awesome!

Unfortunately, it doesn't help with having validation rules per class in a separate file, but it does give you the flexibility of having each block have its own configuration file.

Great post!

Regards,

Dave

__________________________

David Hayden
Microsoft MVP C#
May 30, 2007 at 5:32 PM
nyuser98,

GMariakis' post should fix your problem.

Regards,

Dave

_____________________________________

David Hayden
Microsoft MVP C#
May 30, 2007 at 6:24 PM
Unless I am missing something, one drawback here, which is fairly significant, is that the EntLib Configuration Tools don't have the intelligence to maintain the separate files.

Modifying the configuration ends up dumping everything into the app.config or web.config as opposed to updating the separate files.

Geez...

Regards,

Dave

________________________________

David Hayden
Microsoft MVP C#
May 31, 2007 at 3:09 AM
GMariakis,

Thanks for your suggestion. It works. But I got a following warning in my VS2005. I can ignore it, but just curious if I can remove it or not.
Warning	102	The 'validation' element is not declared.

DavidHayden,
I do agree EntLib Configuration needs some more enhancements and more flexibility on where those files are located and how they are loaded.

OlafConijn,
I have not tried your solution yet. But I will update this post after looking into the ExteranallyConfigurableObjectValidator.

Thanks
May 31, 2007 at 4:52 AM
I am happy it's working for you guys. I too was excited to see this in Nikhil's blog. With respect to the warning. I have not seen it in VS, maybe my settings are a bit more relaxed in that area.

I am not too stunned that VS was unaware of that property of the configSection as it seems that most of us were unaware of it as well. :)

As for the Ent Lib config builder and considering that VS 'missed' it too, lack of support for the separate files is understandable.

I have also run into a few issues with custom classes for cache backingstores and the like, at least they have given us access to the source...maybe when I get some free time I'll try and tackle enhancing it, unless someone beats me to it :).

Regards,
George