jeudi 31 mars 2016

Refactoring of a custom case of validation

I have a hierarchy of classes for validation and i'm at a point that i'd like to refactor (if possible and useful) leaving the code decoupled.

    public class DefaultValidation : ValidationList
    {
       public DefaultValidation(Cutomer customer, Dto dto)
       {
        DefaultRepository repository = new DefaultRepository();

        EntityBook entityBook = (EntityBook)repository.GetBookById(dto.IdBook);
        this.Add(new BookMustBeValidValidation(entityBook));

        EntityVideo entityVideo = (EntityVideo)repository.GetVideoById(dto.IdVideo);
        this.Add(new VideoValidation(entityTask));
        this.Add(new OtherNecessaryValidation(dto.OtherProperty));
       }
    }

This class is a concrete for validation. I add all the rules in a list so i can add various kind of validation in form of classes.

(I used this method that seemed interesting)

The need of a refactory comes when i add another similar class:

    public class SpecialValidation : ValidationList
    {
     public SpecialValidation(Cliente cliente, DtoRichiesta richiesta)
     {
        this.Add(new OtherNecessaryValidation(dto.OtherProperty));
        this.Add(new SpecialAnotherOneValidation(dto.AnotherProperty));
     }
    }

The root is in common, the difference is what classes (rules) i inject to the list. Do you think something should be changed?
Thanks

Aucun commentaire:

Enregistrer un commentaire