DAO with a null object pattern

After reading:
Effective Java (see paragraph 43) - Joshua Bloch
Clear code (don't return zero) - Uncle Bob
Avoid! = Zero statements
Sample Null Object

I was looking for the answer to the question of what the DAO should return when the search ends for an entity that does not exist for objects not associated with the collection. The Collection object is really good, using empty array or emptyList methods. But with non-collections this can be tricky. An alternative solution is to never return null and use the Null Object pattern instead. But I am not going to integrate with the Null Object template with the DAO, and I am very glad to see excellent integration with the Null Object template and the DAO template, especially for the case of returning a model object (dto).

I would appreciate and welcome any best design, script and suggestion.

+5
source share
3 answers

Indeed, the introduction of a null reference is probably one of the worst mistakes in the history of programming languages, even its creator Tony Hoare calls it his billionth mistake.

Here are the best null alternatives to suit your version of Java :

1. Java 8 and higher

Starting with Java 8 you can use java.util.Optional .

Here is an example of how you could use it in your case:

 public Optional<MyEntity> findMyEntity() { MyEntity entity = // some query here return Optional.ofNullable(entity); } 

2. Prior to Java 8

Prior to Java 8, you can use com.google.common.base.Optional from Google Guava .

Here is an example of how you could use it in your case:

 public Optional<MyEntity> findMyEntity() { MyEntity entity = // some query here return Optional.fromNullable(entity); } 
+14
source

All you have to do is return an empty object - say, a client record that you would have in your DAO, for example,

if (result == null) {return new EmptyUser (); }

where EmptyUser extends the User and returns the appropriate entries in getter calls to let the rest of your code know that this is an empty object (id = -1, etc.)

A small example

 public class User { private int id; private String name; private String gender; public String getName() { //Code here } public void setName() { //Code here } } public class EmptyUser extends User { public int getId() { return -1; } public String getName() { return String.Empty(); } } public User getEntry() { User result = db.query("select from users where id = 1"); if(result == null) { return new EmptyUser(); } else { return result; } } 
+6
source

In my experience, in real-world scenarios when a single object needs to be returned, returning zero is actually either a data inconsistency error or lack of data. In both cases, the really good thing to do is to collect and throw your own DataNotFoudException . Crash fast .

I use mybatis as an ORM, and recently I started writing some theoretical options for displaying one result as return lists and checking the amount of data returned in dao and throwing exceptions when the amounts returned do not meet the assumptions of the dao method. It works very well.

+2
source

Source: https://habr.com/ru/post/1258320/


All Articles