how to set the validator configuration when the validation block is used on a class library?

Topics: Validation Application Block
Aug 12, 2014 at 1:12 AM

I have the following scenario:
  • A business logic layer (class library) that makes use of the validation block;
  • A WCF service that uses the business logic layer.
I want the business logic validation to be customizable with configuration, but I am not sure where should I put the configuration.

Should it be on the WCF web.config, or in the business logic class library app.config?

I feel that it should be in the class library app.config, but I am not sure where to call the code to instruct the validation block that it should read the configuration:
ValidationFactory.SetDefaultConfigurationValidatorFactory(new SystemConfigurationSource())
The class library has not entry point to call this.

Is there a known good way of doing it?

Thank you.
Aug 14, 2014 at 7:47 AM
Web.config is the easiest since it's automatically loaded. Personally, I wouldn't usually put the configuration directly in the web.config (especially if it's extensive) but have the web.config point to an external configuration (FileConfigurationSource or configSource). It does impose some configuration requirements on the hosting application but that is .NET configuration for you.

If you want to go with a standalone class library configuration file then you could try to either find an entry point(s) where you can use a static initializer to ensure the configuration is loaded. Or you could force users of the class library to manually bootstrap your class library (good enough for EntLib :) ). Another approach would be to look at Module Initializer to call a method when the assembly is loaded.

Randy Levy
Enterprise Library support engineer
Support How-to
Aug 14, 2014 at 11:59 PM
I've put all the configuration on the service layer (web.config) and it seems to be working. I think it also "feels" better this way. I will look into splitting the entlib configuration on a separate file as you suggested.

Thank You Randy.