mercredi 22 juillet 2015

Verify business rules

I have some rules to validade my model class MyBean. Now it's done by ifs chain inside only one method (validateMyBean). I don't like this way, it seems fuzzy.

What's the best approach for validade many business rules?

public class MyBean {
    private int id;
    private String email;
    private int age;
    private String country;
    private double otherField;
    //getter and setter
}

public class MyBeanFacade {
    //database connection and other methods
}

public class MyBeanBusiness {
    private MyBeanFacade facade;
    private MyBean myBean;

    public boolean validateMyBean() {

        if(!this.myBean.getEmail().contains("@") {
             return false;
        }

        if(this.myBean.getAge()<18 || this.myBean.getAge()>150) {
             return false;
        }

        if(this.myBean.getCountry().startsWith("A") || this.myBean.getCountry().startsWith("B") || this.myBean.getCountry().startsWith("C") || ) {
             return false;
        }

        if(this.myBean.getOtherField() >= 0.123) {
             return false;
        }

        if(facade.existsEmail(myBean.getEmail())) {
             return false;
        }
        return true;
    }
}

Aucun commentaire:

Enregistrer un commentaire