Tags

,

In our application we have input data validation implemented in business layer (separate dll). Because we need to report results to user (our UI layer is ASP.NET MVC application) I came up with following interfaces (simplified version):

It works well until you need to “chain” validators:

public bool Validate(IValidationListener listener)
{
    return firstValidator.Validate(listener) &&
        secondValidator.Validate(listener);
}

Validator stops on first error as it should by it doesn’t collect all warnings because of “short-circuit” evaluation of “&&”. I couldn’t use “&” operator because it would change semantics (i.e. evaluate all validators regardless of possible errors). Instead I wanted something like this:

public bool Validate(IValidationListener listener)
{
    // stops on error but if there are only warnings reports all of them
    return listener.ListenAll(firstValidator, secondValidator);
}

How to do this without changing all implementations of IValidationListener? With extension method and dynamic proxy of course:

🙂

Advertisements