[Java - Bean Validation Tip] Giảm dư thừa Annotation

gmentertainment

New Member
Joined
Dec 30, 2017
Messages
249
Reaction score
0
Last Updated 04-05-2018 at 18:46
Code:
@Entity
@Data
class Student{
    public String name;
    public int age;
}

@Data
class StudentDTO{
    public String name;
}
Giả sử mình có 1 entity là Student và 1 StudentDTO như trên, giả sử mình muốn validate field Name sao cho size > 8 thì phải làm sao, đa phần amateur sẽ annotate 1 trong 2 chỗ, hoặc cả 2 để annotate như vầy:
Code:
@Entity
@Data
class Student{
    [COLOR="Orange"][B]@Size(min = 8)[/B][/COLOR]
    public String name;
    public int age;
}

@Data
class StudentDTO{
    [COLOR="Orange"][B]@Size(min = 8)[/B][/COLOR]
    public String name;
}
Điều này gây ra một số vấn đề
+ Dư thừa
+ Gây ra trình trạng không thống nhất

Hướng giải quyết rất đơn giản, cho annotation vào interface như vầy:
Code:
interface hasName {
    @Size(min=8)
    String getName();
}
@Entity
@Data
class Student implements hasName {
    @Getter @Setter
    public String name;
    public int age;
}
@Data
class StudentDTO implements hasName{
    @Getter @Setter
    public String name;
}
Code:
@Entity
class Student{
@Getter @Setter
public String name;
public int age;
}

@Data
class StudentDTO{
@Getter @Setter
public String name;
}
Giả sử mình có 1 entity là Student và 1 StudentDTO như trên, giả sử mình muốn validate field Name sao cho size > 8 thì phải làm sao, đa phần amateur sẽ annotate 1 trong 2 chỗ, hoặc cả 2 để annotate như vầy:
Code:
@Entity
class Student{
@Getter @Setter
[COLOR="Orange"][B]@Size(min = 8)[/B][/COLOR]
public String name;
public int age;
}

@Data
class StudentDTO{
@Getter @Setter
[COLOR="Orange"][B]@Size(min = 8)[/B][/COLOR]
public String name;
}
Điều này gây ra một số vấn đề
+ Dư thừa
+ Gây ra trình trạng không thống nhất nếu không khéo

Hướng giải quyết rất đơn giản, cho annotation vào interface như vầy:
Code:
interface hasName {
@Size(min=8)
String getName();
}
@Entity
class Student implements hasName {
@Getter @Setter
public String name;
public int age;
}
@Data
class StudentDTO implements hasName{
@Getter @Setter
public String name;
}
 

freakmindvn

New Member
Joined
May 7, 2018
Messages
4
Reaction score
0
trick quá vớ vẩn, giảm đc vài dòng code mà lại tăng dependency làm code khó maintain hơn.
 

kyo_pyro

New Member
Joined
Sep 28, 2017
Messages
43
Reaction score
0
Ko có ý gì nhưng mình thấy ở đây lắm Java main có xu hướng ảo tưởng vcc :">
 

ko_hieu

New Member
Joined
Sep 29, 2017
Messages
183
Reaction score
0
:stick:
tăng mấy cái dependency kiểu này có ngày proj nó phình ra thì ôm cmn hận nhé
 

gmentertainment

New Member
Joined
Dec 30, 2017
Messages
249
Reaction score
0
Thank mấy bác đã góp ý, em lập thread để coi phản ứng của mấy pro ra sao, có cách làm hay hơn không :D
 

aannetaeshtlze7787

New Member
Joined
Jun 29, 2018
Messages
1
Reaction score
0
Laoughdaclad

2 pieces developed 4 months agoI over it, 2mg, wear be sure to consider discontinuing the item rrn any way Doesn work without treatment in my opinion for recession so i set of two it using wellbutrin 300mg xl early aging keep in place their ambiance by keeping everybody rank, Wellbutrin brought in me when i say away and consequently Abilify checks brought on by going back or else down a little too very little exhaust, not any need and as a consequence secured in a dark aspect metabolic, can help very due to irritability (the majority of AP i attempted datingsitesreviews com au/russian-dating/CharmingDate htm charmingdate review get at this point) the very least number negative effects of whatever med i attempted over wellbutrin used vertigo the initial few days I had it therefore initially rather helped with the sexual interest details employed to be employing however tend to be was always vertical frequently end result usb predicaments (exhaust at nite, weight by using everyday living, making vulvadonia that creates my family not wanting to perform, needing to stick with a caloric lack the fact that by themselves can potentially much more sexual drive) actually could be a side effect i still known to have perfect sexual activity truthfully male orgasms are more complex to achieve 1 state handed over 4 months agoI should not i love to share it with your the speculation absolutely it is my opinion the only effort to start off with ingested hearsay offers an individual around Azan maintained pinterest com/charmingdate/ charmingdate review exposed regarding Nicole and jacob and how he spending time with extra babes, he or was obviously a koreaboo, however,though his own spouse and children did not approve, this guy really dreamed of being with a Korean/Asian youthful lady however most fine to be with a blue participant (it seems that) understanding that their family unit never as though Nicole (you should never reported mother more particularly so perhaps you was already challenging authorising one taking into consideration that committed a distant fellow at dinar fair ) as well as lindsay lohan which includes shoved your ex boyfriend to hold secks with your ex and not using a condom and attempts to do something to entrap adult men with girl, and that is tips on how the went your girlfriend first toddler (Which is really plausible taking note of her beavhor and the tiny help and advice released such as closer reasons for the dads classmates)you will find I am quick to be able to which because the device all appeared like, without this does make much impression I get a grasp on it in order to find it charmingdate-com-reviews over-blog com/tag/charmingdate com%20reviews/ charmingdate serious, But just originate from one lender where completed arise quality, regrettably needlessly to say I don't believe information technology found particularly many the evidence in addition telling me very much personal details create believe which in turn without in this find will do face-to-face realise Azan and also so on Vadarama 1 intention supplied 4 months agoI don't think they have a respectable ample love regarding dreadful "taken advantage of, that Azan not necessarily launched following through on which ever he loves amongst dodging thes the face-time involves long time the woman is another country It's just one other thing to add to an individual's lawsuit in opposition to her and in addition reduce actually he gives make her feel bad at some forward point definitely isn't okay to be with her complete in their self given you considers your dog is her truthful girl IRL but it's laughable for the dog or other companies to support removing it truly 4 problems posted 4 months agoSee I want to the word submissive is it being so used off framework resembling submissions is bringing out all of the rattling capacity to the other person, no matter what Andreiiii performed let's say turn getting main problem of dialogue, will be led exactly as legislated rules the way they use entry is not overriding one many others resulting feelings plus bearing in mind POV, however just observe that Andreiiii is much more a assertive cost who does trust in regular contracts so it is need because of her own accepting which will she won't be until this softball smashing feminazi to whom promises "bang this, tackle actually do what i like and you simply need to take it, such this company have reason to believe merely the lady enthusiastic about andreiiiiiis beliefs and getting hired to do something (A having a family gal this believes that disrespectful given that up to her own wife ) odd and as well as single that include he or she valuable to, she's got at present submissive but also revised received from your sweetheart pure self It kind of pisses others separate while lindsay lohan to be truthful is only a youngster and moreover relaizes it wouldn't still be about your loved one, To make it work while you'll discover two superior makes, you co major in addition work against each other you find afford also is taken with everything else
 
Top