mercredi 18 décembre 2019

Incapsulating class with static wrapper method is good idea?

I have been refactoring a huge method in the project I work and came up with this idea to create a validation service like this -

public class TrickyValidation {
    String validationVariable1;
    String validationVariable2;
    String validationVariable3;

    Object itemsMap;
    Object dependentObject;

    @Autowired
    SpringService service;

    public static boolean doTrickyValidation(HashMap<String, Object> itemsMap, Object dependentObject) {
        return new TrickyValidation(itemsMap, dependentObject).validate();
    }

    private TrickyValidation(Object itemsMap, Object dependentObject) {
        this.itemsMap = itemsMap;
        this.someDependentObject = dependentObject;
        init();
    }

    private boolean validate() {
        // loads of logic for validation by using trickyVars
        return true;
    }

    private void init() {
        // Some methods to extract thease variables from itemsMap, dependentObject etc..
       this.validationVariable1 = service.get(dependentObject); 
       this.validationVariable1 = ...;
       this.validationVariable1 = ...;
    }
}

My goal what I want to do here is to Encapsulate everything as much as possible and use clean code principles.

  • I feel a bit here like fighting spring framework because I don't want that "TrickyValidation" class would be @Servcie and belong to spring container. Will Autowired even work here?

  • Is it a good design? Most likely I will use this validation in a loop. I like this solution because when I have to validate things I just simply call one and only public static method of this class TrickyValidation.doTrickyValidation(map, obj)

Any suggestions are welcome on how to improve this, or why it's a bad idea.

Aucun commentaire:

Enregistrer un commentaire