Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation strategies

I have a business model with many classes in, some logical entities within this model consist of many different classes (Parent-child-grandchild.) On these various classes I define constraints which are invariant, for example that the root of the composite should have a value for Code.

I currently have each class implement an interface like so...

public interface IValidatable
{
    IEnumerable<ValidationError> GetErrors(string path);
}

The parent would add a validation error if Code is not set, and then execute GetErrors on each child, which in turn would call GetErrors on each grandchild.

Now I need to validate different constraints for different operations, for example

  1. Some constraints should always be checked because they are invariant
  2. Some constraints should be checked when I want to perform operation X on the root.
  3. Some additional constraints might be checked when performing operation Y.

I have considered adding a "Reason" parameter to the GetErrors method but for a reason I can't quite put my finger on this doesn't feel right. I have also considered creating a visitor and having a concrete implementation to validate for OperationX and another for OperationY but dislike this because some of the constraint checks would be required for multiple operations but not all of them (e.g. a Date is required for OperationX+OperationY but not OperationZ) and I wouldn't like to have to duplicate the code which checks.

Any suggestions would be appreciated.

like image 870
Peter Morris Avatar asked Jul 06 '11 16:07

Peter Morris


3 Answers

You have an insulation problem here, as your classes are in charge of doing their own validation, yet the nature of that validation depends on the type of operation you're doing. This means the classes need to know about the kinds of operations that can be performed on them, which creates a fairly tight coupling between the classes and the operations that use them.

One possible design is to create a parallel set of classes like this:

public interface IValidate<T>
{
    IEnumerable<ValidationError> GetErrors(T instance, string path);
}

public sealed class InvariantEntityValidator : IValidate<Entity>
{
    public IEnumerable<ValidationError> GetErrors(Entity entity, string path)
    {
        //Do simple (invariant) validation...
    }
}

public sealed class ComplexEntityValidator : IValidate<Entity>
{
    public IEnumerable<ValidationError> GetErrors(Entity entity, string path)
    {
        var validator = new InvariantEntityValidator();
        foreach (var error in validator.GetErrors(entity, path))
            yield return error;

        //Do additional validation for this complex case
    }
}

You'll still need to resolve how you want to associate the validation classes with the various classes being validated. It sounds like this should occur at the level of the operations somehow, as this is where you know what type of validation needs to occur. It's difficult to say without a better understanding of your architecture.

like image 89
Dan Bryant Avatar answered Oct 21 '22 08:10

Dan Bryant


I would do a kind of attribute-based validation:

public class Entity
{
    [Required, MaxStringLength(50)]
    public string Property1 { get; set; }  

    [Between(5, 20)]
    public int Property2 { get; set; }

    [ValidateAlways, Between(0, 5)]
    public int SomeOtherProperty { get; set; }      

    [Requires("Property1, Property2")]
    public void OperationX()
    {
    }
}

Each property which is passed to the Requires-attribute needs to be valid to perform the operation.

The properties which have the ValidateAlways-attribute, must be valid always - no matter what operation.

In my pseudo-code Property1, Property2 and SomeOtherProperty must be valid to execute OperationX.

Of course you have to add an option to the Requires-attribute to check validation attributes on a child, too. But I'm not able to suggest how to do that without seeing some example code.

Maybe something like that:

[Requires("Property1, Property2, Child2: Property3")]

If needed you can also reach strongly typed property pointers with lambda expressions instead of strings (Example).

like image 22
Arxisos Avatar answered Oct 21 '22 08:10

Arxisos


I would suggest using the Fluent Validation For .Net library. This library allows you to setup validators pretty easily and flexibly, and if you need different validations for different operations you can use the one that applies for that specific operation (and change them out) very easily.

like image 32
KallDrexx Avatar answered Oct 21 '22 09:10

KallDrexx