3 ms·
Why write many lines of validation when one line can suffice? @Entity class User { @NotBlank String username; } // Use site
by al_mandi 4y ago
Why write many lines of validation when one line can suffice?
@Entity
class User {
@NotBlank
String username;
}
// Use site
public User addUser(@Valid User user) { ... }
as opposed to
class User {
String username;
public void validate() throws ValidationException {
if (username.isBlank()) throw new ValidationException("username is empty");
}
}
// Use site
public User addUser(User user) {
user.validate();
// ...
}
- cutler 4y agoI was thinking more about @RequestParam validation eg. fun calc1(@RequestParam @NotNull @Size(max=10) @Pattern(regexp = "^\\d{4}-\\d{1,2}-\\d{1,2}$") date:String, @RequestParam @NotNull @Size(max=5) @Pattern(regexp = "^\\d{2}:\\d{2}$") time:String, @RequestParam @NotNull @Size(max=40) @Pattern(regexp = "^[-_'A-Za-z/\\d]{2,50}$") zone:String, ): Map<String, Any> { Why stuff everything in the param list? No other framework I can think of proliferates annotations like this. Spring annotations being Java-based means I must suffer Java's ridiculous inability to handle regex metacharacters - even after introducing raw string literals in Java 13. Kotlin handles this perfectly but once I'm in Spring annotation-land Spring's touted Kotlin compatibility goes out the window.
- al_mandi 4y agoSure, but nothing is stopping you from writing a `validate()` method right? It also appears to me that having so many validations for a base type `String` is indicative that there should be a `Date`, `Time`, and `Zone` type respectively for each of the arguments.
- mesarvagya 4y agoCode like this is expected in Spring https://hastebin.com/jevibugiqu.less https://hastebin.com/jevibugiqu.less . Literally all the classes I see are filled with annotations. Can we even write code in pure java without a single annotation ? Theres a lot of magic going on with annotation. It works great while it is running. God forbid there is some issue and we have to figure it out where all the magic is happening.
- KronisLV 4y agoThat is actually a JPA mapping for your persistence layer, which is one of the relatively few things where lots of annotations should be acceptable (except for the inevitable runtime problems due to foreign data mappings): https://en.wikipedia.org/wiki/Jakarta_Persistence https://en.wikipedia.org/wiki/Jakarta_Persistence It's not even that bad, when you consider something like myBatis, which can be useful for cases where you need complex dynamic SQL for your models, but can also involve boilerplate if you don't have codegen: https://mybatis.org/mybatis-3/sqlmap-xml.html https://mybatis.org/mybatis-3/sqlmap-xml.html Though in regards to service code and such, I agree, it can get out of hand and be a total mess sooner rather than later. Not being able to properly debug this magic is just the cherry on the top.
- cutler 4y agoIt was the @Query annotation which finally killed Spring for me. So I have to stuff all my SQL into an annotation now if I want raw JDBC? Madness. **ck that.
- kaba0 4y agoWhy would you have to do that? You can just inject an entitymanager to wherever you want to use it natively and do whatever you want.
- cutler 4y ago"inject an entitymanager"?? I don't even have any entities, just a db I want to query with raw sql.
- nick__m 4y agoNothing in Spring-boot force Hibernate upon you. I stated that I used to use mybatis, a resultset mapper... it's a lot closer to JDBC and if you really need raw JDBC, have a datasource injected where it's needed.