java - Verify business rules -
i have rules validade model class mybean. it's done ifs chain inside 1 method (validatemybean). don't way, seems fuzzy.
what's best approach validade many business rules?
public class mybean { private int id; private string email; private int age; private string country; private double otherfield; //getter , setter } public class mybeanfacade { //database connection , 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; } }
put validation logic setter methods, prevent invalid values ever being set. throw exception reject given value. eliminates need validation method, , guarantees validation code run every set value.
better yet, eliminate setters taking values in constructor, guarantee validation executed. if there many parameters 1 constructor, make use of builder pattern.
Comments
Post a Comment