mardi 11 décembre 2018

How to validate a complex model properly

I'm looking for a scalable way to validate a complex model. This model contains properties with more complex models which also need to be validated. The validations I need to do have a lot of external dependencies which are implemented in the validation classes by dependency injection.

I recently made multiple validator classes for each model. This way I could seperate the code in different sections which I afterwards 'lock' into eachother.

Example:

class FooClass { public BarClass Bar {get;set; } }

validation class:

class ValidatorFooClass {
   private readonly ValidatorBarClass _validatorBarClass;
   //Constructor depenency injection resulting in -> ValidatorBarClass        

    public FooValidation Validate(FooClass foo){
             var fooValidation = new FooValidation();

             // Validate foo stuff
             ...
             // Validate bar stuff
             fooValidation.BarValidation = _validatorBarClass.Validate(foo.Bar);
     }
  }

Though it works I'm looking for a more maintenable way...

Aucun commentaire:

Enregistrer un commentaire