Validation block, MessageTemplate from custom ResourceManager

Topics: Validation Application Block
Nov 9, 2010 at 2:38 PM
Edited Nov 9, 2010 at 4:00 PM

Hello, in the validation block, we can override the generic, default validation message templates using custom localized resources.

However,  I want to get the localized message template resources not from an assembly file but from a database. I have implemented a custom ResourceManager for this.

How can I accomplisch this for the validation block.


Nov 9, 2010 at 10:58 PM

Unfortunately, you will need to modify the entlib source code as there's no extension point yet for using a custom resource manager.  The ResourceStringLoader class from the Common project explicitly uses the ResourceManager class. 

By the way, there's already a logged feature request for this in the Issue Tracker.  You can vote for it.


Sarah Urmeneta
Global Technologies & Solutions
Avanade, Inc.

Nov 10, 2010 at 9:55 AM

There is a workaround that might work for you. Instead of using the resources directly, you can use identifiers of your resource database as validation message and use them to fetch the real (localized) messages, before showing it to the user. There are several ways of doing this. A solution might look like this:

public static class Validator
    public static void Validate(object entity)
        var validator = ValidationFactory.GetValidator(entity.GetEntity());

        var results = validator.Validate(entity);

        if (!results.IsValid)
            results = LocalizeResults(results);

            throw new ValidationException(results);

    private static ValidationResults LocalizeResults(ValidationResults results)
        var localizedResults = new ValidationResults();


        return localizedResults;

    private static IEnumerable<ValidationResult> LocalizeResultsRecursive(
        IEnumerable<ValidationResult> results)
        foreach (var result in results)
            var localizedMessage = GetLocalizedMessageById(result.Message);

            var subResults =

            localizedResults.Add(new ValidationResult(localizedMessage, 
                result.Target, result.Key, result.Tag, result.Validator,

    private static string GetLocalizedMessageById(string messageId)
       var culture = CultureInfo.CurrentCulture;
       // TODO: Fetch the text from the resource system.
I hope this helps.

Nov 10, 2010 at 3:46 PM

Hi dot_net_junkie

i think that won't work because the messages are "dynamic" (using a string.format)

where the message template i want to translate is something like "string length must be between {0} and {1} characters"

with the system you propose, this would result in too many possible combinations that each require a translation

"string length must be between 1 and 10 characters"

"string length must be between 0 and 50 characters"


kind regards




Nov 11, 2010 at 8:41 AM
Edited Nov 11, 2010 at 8:43 AM


Try writing your validators as follows::


The first argument (53) is the identifier in your resource database. You can write the GetLocalizedMessageById method as follows:

private static string GetLocalizedMessageById(string messageTemplate)
    int index = messageTemplate.IndexOf('/');

    string messageId = messageTemplate.Substring(0, index);

    object[] arguments = messageTemplate.Substring(index + 1).Split('/');

    string localizedMessageTemplate = GetLocalizedMessageTemplate(messageId);

    return string.Format(localizedMessageTemplate, arguments);

private static string GetLocalizedMessageTemplate(string messageId)
    var culture = CultureInfo.CurrentCulture;

    // TODO: Fetch the text from the resource system.

The resource with id 53 could look like this:

       string length must be between {3} and {5} characters



Nov 19, 2010 at 2:24 PM

Thats works just fine, thanks a lot