Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating entities rules

I'd like to know the answer to this simple question.

When I create an entity object and I want to restrict a setting of an attribute (for example I don't want to allow anyone to set an integer value less then 1 to an attribute), should I implement it in the setter of this attribute or should I check this restriction latter in a class that handles these objects ? Generally, can I implement getters and setters however I want as long as my getters return and setters set attributes ?

I know there are some rules (code conventions) in java, so I don't want to break any of them.

Thanks in advance, hope that my question is clear enough and sorry for any grammar mistakes I might have made :/ .

like image 585
VaclavDedik Avatar asked Aug 09 '11 17:08

VaclavDedik


People also ask

What are the rule of entity?

Entity Rules provides an alternative method of triggering Rules. Instead of adding entity related events to Reaction Rules you are able to select which special Rules components will fire when different entity events are triggered.

What does it mean to create an entity?

What is creation of a business entity? Creation of a business entity is the legal or procedural steps that one must undertake to bring the business entity into existence. There is a general dichotomy in the process or steps required to form a business entity.


1 Answers

Yes getters/setters are useful for that.

for example:

public void setAge(int age){
 if(age < 0){
  throw new IllegalArgumentException("Invalid age : " + age);
  //or if you don't want to throw an exception you can handle it otherways too
 }
}

You can also use Java-EE's Bean Validators for this

public class Person{

   @Min(value = 0)
   @Max(value = 99)
   private Integer age;

   //some other code
}
like image 51
jmj Avatar answered Nov 15 '22 11:11

jmj