Validation Inheritance and Composition

Considering the class inheritance OOP principle and following the best practices and principles of software design, of the code re-usability, there are various scenarios where inherited types have the same validation as their base types.
Therefore, the Generic Validation.NET has great support for inheritance (following class inheritance), and it is very simple to setup this.
public EmployeeValidation(IValidationFactory factory)
{
    //inheriting from Validation<User>
    Inherit<User>(factory);
    .....
}

In this case the Validation<T> constructor must have a IValidationFactory parameter, that would be further used to determine the Validation<U> registered for the base type "U".
The IValidationFactory is the component that handles the Validation<> lifetime.

Messaging with Custom Member Names

The built-in default Member Validators that come with Generic Validation.NET generally have a standard error message that also contain the validated member name. This is not always desirable, since the validated members may not always have friendly names (not to mention the composite names like "UserName", "LastName").
Therefore, it is possible to use custom names for the built in member validators through the .NET Globalization resourcing.
For each custom member, the resource key must be the member name and the resource value the member friendly/display name (ex. key="UserName", Value="User Name").
public UserValidation()
{
     LocalizeMembers(Members.ResourceManager);
     ...
}

Validation Globalization - Localization of Error Messages

Generic Validation.NET has real support for Globalization and localized error messages. AS you have seen previously, it is possible to localize the member names, but it is also possible to localize the error messages.
For any Globalization scenarios the Generic Validation.NET uses "ResourceManager" objects.

Important: Validation messages can be formatted strings with placeholders, ex: Salary {0} is not valid with {1} position. When setting a validation message it is possible to use any member runtime value of the validated object

First of all, you need to register the "ResourceManager" into the Validation<> definition:
public EmployeeValidation(IValidationFactory factory)
{
    //inheriting from Validation<User>
    Inherit<User>(factory);

    //Enable Localization/Globalization: Employee member names: Age, CompanyName (to be further used in the member validation messages)
    LocalizeMembers(Members.ResourceManager);

    //Enable Localization/Globalization: setup resource manager(s) for the validation messages
    LocalizeMessages(Messages.ResourceManager);
    .....
}

Then, just register the Validators (Member and Object validators):
Member(p => p.Salary)
    .IsRequired()
    .Message("InvalidSalary", p => p.Salary)
    .Always();

The registered validation message "InvalidSalary" is considered as a key of the ResourceManager (ex. key="InvalidSalary", value="Salary {0} is not valid without a company.")

Last edited Jun 24, 2010 at 9:14 AM by adyi04, version 1

Comments

No comments yet.