|
9 | 9 | --- |
10 | 10 |
|
11 | 11 | ## Intent |
12 | | -Object provides an abstract interface to some type of database or |
13 | | -other persistence mechanism. |
| 12 | +Object provides an abstract interface to some type of database or other persistence mechanism. |
| 13 | + |
| 14 | +## Explanation |
| 15 | + |
| 16 | +Real world example |
| 17 | + |
| 18 | +> There's a set of customers that need to be persisted to database. Additionally we need the whole set of CRUD (create/read/update/delete) operations so we can operate on customers easily. |
| 19 | +
|
| 20 | +In plain words |
| 21 | + |
| 22 | +> DAO is an interface we provide over the base persistence mechanism. |
| 23 | +
|
| 24 | +Wikipedia says |
| 25 | + |
| 26 | +> In computer software, a data access object (DAO) is a pattern that provides an abstract interface to some type of database or other persistence mechanism. |
| 27 | +
|
| 28 | +**Programmatic Example** |
| 29 | + |
| 30 | +Walking through our customers example, here's the basic Customer entity. |
| 31 | + |
| 32 | +```java |
| 33 | +public class Customer { |
| 34 | + |
| 35 | + private int id; |
| 36 | + private String firstName; |
| 37 | + private String lastName; |
| 38 | + |
| 39 | + public Customer(int id, String firstName, String lastName) { |
| 40 | + this.id = id; |
| 41 | + this.firstName = firstName; |
| 42 | + this.lastName = lastName; |
| 43 | + } |
| 44 | + |
| 45 | + public int getId() { |
| 46 | + return id; |
| 47 | + } |
| 48 | + |
| 49 | + public void setId(final int id) { |
| 50 | + this.id = id; |
| 51 | + } |
| 52 | + |
| 53 | + public String getFirstName() { |
| 54 | + return firstName; |
| 55 | + } |
| 56 | + |
| 57 | + public void setFirstName(final String firstName) { |
| 58 | + this.firstName = firstName; |
| 59 | + } |
| 60 | + |
| 61 | + public String getLastName() { |
| 62 | + return lastName; |
| 63 | + } |
| 64 | + |
| 65 | + public void setLastName(final String lastName) { |
| 66 | + this.lastName = lastName; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public String toString() { |
| 71 | + return "Customer{" + "id=" + getId() + ", firstName='" + getFirstName() + '\'' + ", lastName='" |
| 72 | + + getLastName() + '\'' + '}'; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean equals(final Object that) { |
| 77 | + var isEqual = false; |
| 78 | + if (this == that) { |
| 79 | + isEqual = true; |
| 80 | + } else if (that != null && getClass() == that.getClass()) { |
| 81 | + final var customer = (Customer) that; |
| 82 | + if (getId() == customer.getId()) { |
| 83 | + isEqual = true; |
| 84 | + } |
| 85 | + } |
| 86 | + return isEqual; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public int hashCode() { |
| 91 | + return getId(); |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +Here's the DAO interface and two different implementations for it. InMemoryCustomerDao keeps a simple map of customers |
| 97 | +in memory while DBCustomerDao is the real RDBMS implementation. |
| 98 | + |
| 99 | +```java |
| 100 | +public interface CustomerDao { |
| 101 | + |
| 102 | + Stream<Customer> getAll() throws Exception; |
| 103 | + |
| 104 | + Optional<Customer> getById(int id) throws Exception; |
| 105 | + |
| 106 | + boolean add(Customer customer) throws Exception; |
| 107 | + |
| 108 | + boolean update(Customer customer) throws Exception; |
| 109 | + |
| 110 | + boolean delete(Customer customer) throws Exception; |
| 111 | +} |
| 112 | + |
| 113 | +public class InMemoryCustomerDao implements CustomerDao { |
| 114 | + |
| 115 | + private Map<Integer, Customer> idToCustomer = new HashMap<>(); |
| 116 | + |
| 117 | + @Override |
| 118 | + public Stream<Customer> getAll() { |
| 119 | + return idToCustomer.values().stream(); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public Optional<Customer> getById(final int id) { |
| 124 | + return Optional.ofNullable(idToCustomer.get(id)); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public boolean add(final Customer customer) { |
| 129 | + if (getById(customer.getId()).isPresent()) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + idToCustomer.put(customer.getId(), customer); |
| 134 | + return true; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public boolean update(final Customer customer) { |
| 139 | + return idToCustomer.replace(customer.getId(), customer) != null; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public boolean delete(final Customer customer) { |
| 144 | + return idToCustomer.remove(customer.getId()) != null; |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +public class DbCustomerDao implements CustomerDao { |
| 149 | + |
| 150 | + private static final Logger LOGGER = LoggerFactory.getLogger(DbCustomerDao.class); |
| 151 | + |
| 152 | + private final DataSource dataSource; |
| 153 | + |
| 154 | + public DbCustomerDao(DataSource dataSource) { |
| 155 | + this.dataSource = dataSource; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public Stream<Customer> getAll() throws Exception { |
| 160 | + try { |
| 161 | + var connection = getConnection(); |
| 162 | + var statement = connection.prepareStatement("SELECT * FROM CUSTOMERS"); |
| 163 | + var resultSet = statement.executeQuery(); |
| 164 | + return StreamSupport.stream(new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE, |
| 165 | + Spliterator.ORDERED) { |
| 166 | + |
| 167 | + @Override |
| 168 | + public boolean tryAdvance(Consumer<? super Customer> action) { |
| 169 | + try { |
| 170 | + if (!resultSet.next()) { |
| 171 | + return false; |
| 172 | + } |
| 173 | + action.accept(createCustomer(resultSet)); |
| 174 | + return true; |
| 175 | + } catch (SQLException e) { |
| 176 | + throw new RuntimeException(e); |
| 177 | + } |
| 178 | + } |
| 179 | + }, false).onClose(() -> mutedClose(connection, statement, resultSet)); |
| 180 | + } catch (SQLException e) { |
| 181 | + throw new CustomException(e.getMessage(), e); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private Connection getConnection() throws SQLException { |
| 186 | + return dataSource.getConnection(); |
| 187 | + } |
| 188 | + |
| 189 | + private void mutedClose(Connection connection, PreparedStatement statement, ResultSet resultSet) { |
| 190 | + try { |
| 191 | + resultSet.close(); |
| 192 | + statement.close(); |
| 193 | + connection.close(); |
| 194 | + } catch (SQLException e) { |
| 195 | + LOGGER.info("Exception thrown " + e.getMessage()); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + private Customer createCustomer(ResultSet resultSet) throws SQLException { |
| 200 | + return new Customer(resultSet.getInt("ID"), |
| 201 | + resultSet.getString("FNAME"), |
| 202 | + resultSet.getString("LNAME")); |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public Optional<Customer> getById(int id) throws Exception { |
| 207 | + |
| 208 | + ResultSet resultSet = null; |
| 209 | + |
| 210 | + try (var connection = getConnection(); |
| 211 | + var statement = connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) { |
| 212 | + |
| 213 | + statement.setInt(1, id); |
| 214 | + resultSet = statement.executeQuery(); |
| 215 | + if (resultSet.next()) { |
| 216 | + return Optional.of(createCustomer(resultSet)); |
| 217 | + } else { |
| 218 | + return Optional.empty(); |
| 219 | + } |
| 220 | + } catch (SQLException ex) { |
| 221 | + throw new CustomException(ex.getMessage(), ex); |
| 222 | + } finally { |
| 223 | + if (resultSet != null) { |
| 224 | + resultSet.close(); |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + public boolean add(Customer customer) throws Exception { |
| 231 | + if (getById(customer.getId()).isPresent()) { |
| 232 | + return false; |
| 233 | + } |
| 234 | + |
| 235 | + try (var connection = getConnection(); |
| 236 | + var statement = connection.prepareStatement("INSERT INTO CUSTOMERS VALUES (?,?,?)")) { |
| 237 | + statement.setInt(1, customer.getId()); |
| 238 | + statement.setString(2, customer.getFirstName()); |
| 239 | + statement.setString(3, customer.getLastName()); |
| 240 | + statement.execute(); |
| 241 | + return true; |
| 242 | + } catch (SQLException ex) { |
| 243 | + throw new CustomException(ex.getMessage(), ex); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + @Override |
| 248 | + public boolean update(Customer customer) throws Exception { |
| 249 | + try (var connection = getConnection(); |
| 250 | + var statement = |
| 251 | + connection |
| 252 | + .prepareStatement("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?")) { |
| 253 | + statement.setString(1, customer.getFirstName()); |
| 254 | + statement.setString(2, customer.getLastName()); |
| 255 | + statement.setInt(3, customer.getId()); |
| 256 | + return statement.executeUpdate() > 0; |
| 257 | + } catch (SQLException ex) { |
| 258 | + throw new CustomException(ex.getMessage(), ex); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + @Override |
| 263 | + public boolean delete(Customer customer) throws Exception { |
| 264 | + try (var connection = getConnection(); |
| 265 | + var statement = connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) { |
| 266 | + statement.setInt(1, customer.getId()); |
| 267 | + return statement.executeUpdate() > 0; |
| 268 | + } catch (SQLException ex) { |
| 269 | + throw new CustomException(ex.getMessage(), ex); |
| 270 | + } |
| 271 | + } |
| 272 | +} |
| 273 | +``` |
| 274 | + |
| 275 | +Finally here's how we use our DAO to manage customers. |
| 276 | + |
| 277 | +```java |
| 278 | + final var dataSource = createDataSource(); |
| 279 | + createSchema(dataSource); |
| 280 | + final var customerDao = new DbCustomerDao(dataSource); |
| 281 | + |
| 282 | + addCustomers(customerDao); |
| 283 | + log.info(ALL_CUSTOMERS); |
| 284 | + try (var customerStream = customerDao.getAll()) { |
| 285 | + customerStream.forEach((customer) -> log.info(customer.toString())); |
| 286 | + } |
| 287 | + log.info("customerDao.getCustomerById(2): " + customerDao.getById(2)); |
| 288 | + final var customer = new Customer(4, "Dan", "Danson"); |
| 289 | + customerDao.add(customer); |
| 290 | + log.info(ALL_CUSTOMERS + customerDao.getAll()); |
| 291 | + customer.setFirstName("Daniel"); |
| 292 | + customer.setLastName("Danielson"); |
| 293 | + customerDao.update(customer); |
| 294 | + log.info(ALL_CUSTOMERS); |
| 295 | + try (var customerStream = customerDao.getAll()) { |
| 296 | + customerStream.forEach((cust) -> log.info(cust.toString())); |
| 297 | + } |
| 298 | + customerDao.delete(customer); |
| 299 | + log.info(ALL_CUSTOMERS + customerDao.getAll()); |
| 300 | + |
| 301 | + deleteSchema(dataSource); |
| 302 | +``` |
| 303 | + |
14 | 304 |
|
15 | 305 | ## Class diagram |
16 | 306 |  |
|
0 commit comments