1

I would like to use my own localization implementation instead of the default resource file system the framework provides. I need also to configure the validators in a configuration file.

My problem is that I don't know how to access the Validator objects to change their message templates after I have instantiated the ConfigurationValidatorFactory. I can create a validator for my object like this:

var cfgSrc = new FileConfigurationSource("Validations.xml");
var factory = ConfigurationValidatorFactory.FromConfigurationSource(cfgSrc);
Validator val = factory.CreateValidator<MyObj>();

, but 'val' above is then of type GenericValidatorWrapper and has no properties to access the 'true validator' instances.

After validation I can see the true Validator instances, but then it is too late to change their template text. The final message (containing the limit values) has then already been created and changing the template won't re-create the message.

Any suggestions?

Enterprise Library version is 5.

Randy Levy
  • 22,566
  • 4
  • 68
  • 94
friskm
  • 366
  • 3
  • 7

1 Answers1

0

You might be able to use reflection to modify the validator properties that you are interested in. But that is really messy and Kludgy.

What if you simply replaced the message text on the validation results after validation?

The message in the configuration file could be a place holder (e.g. NAME_INVALID_LENGTH) and you could replace that with your actual text.

var cfgSrc = new FileConfigurationSource("Validations.xml");
var factory = ConfigurationValidatorFactory.FromConfigurationSource(cfgSrc);
Validator val = factory.CreateValidator<MyObj>();

MyObj myObj = new MyObj();
myObj.Name = "Swiper McFoxy";

ValidationResults results = new ValidationResults();

foreach (ValidationResult result in val.Validate(myObj))
{
    results.AddResult(
        new ValidationResult(
            GetMessage<MyObj>(result.Message, new CultureInfo("en")),
            result.Target,
            result.Key,
            result.Tag,
            result.Validator,
            result.NestedValidationResults));
}

Update

OK, if you need the template text you can investigate poking around via reflection but that still looks really messy.

Given your requirements (configuration file, maintain templates) what I would probably do would be to create multiple configuration files each with the template text you want.

That's a bit of a maintenance nightmare so I would refine that idea to:

  1. Create my configuration file Validations.xml and instead of putting my templated text in I would use a unique placeholder for each message (e.g. NAME_INVALID_LENGTH but even more unique-ish).

  2. Then I would write a small piece of code/tool that would, for every locale you are supporting, replace the unique placeholder with the locale specific templated text. And copy the resultant file to a locale specific folder under the OutDir (e.g. bin\debug\en-US\Validations.xml). This is similar to how .NET deploys satellite assemblies so it is not that far a stretch.

  3. Run the tool above as a post-build step. The tool can flag placeholders that it can't find so that it gets reported during the build. This should help keep the placeholders and the template text in sync.

  4. Modify your code to use the locale specific configuration file:

    CultureInfo culture = new CultureInfo("en-us");
    var cfgSrc = new FileConfigurationSource(culture.Name + @"\Validations.xml");
    var factory = ConfigurationValidatorFactory.FromConfigurationSource(cfgSrc);
    Validator val = factory.CreateValidator<MyObj>();
    

    You can abstract the culture specific code behind a helper method so that it's easier to read and maintain. e.g. GetValidationConfigurationSource(culture).

Randy Levy
  • 22,566
  • 4
  • 68
  • 94
  • Thanks. I thought about the message (or tag) usage too but then you cannot use the templating feature (cannot use placeholders) in the localized message. – friskm Jan 12 '11 at 13:40
  • I got an answer here: http://entlib.codeplex.com/Thread/View.aspx?ThreadId=241350. (This way I can use the template texts which is critical in my case.) – friskm Jan 12 '11 at 17:23
  • That was the same idea I had! Except I'm generating the config files to avoid the config duplication. – Randy Levy Jan 12 '11 at 17:49