title: Single DAO for webservice and database
body: <p>I am writing my first Spring MVC webapp and have a question about DAOs and web service requests.</p>
  
  <p>Essentially my app allows the user to construct an order, which gets persisted to a database via a DAO. Later on a scheduled worker will retrieve the new orders from the database, submit them to a third-party SOAP service and update each order with some details (e.g. order number).</p>
  
  <p>My controller calls the OrderService (a condensed version):</p>
  
  <pre><code>@Service
  public class OrderService {
  
      @Autowired
      private OrderDao orderDao;
  
      public List&lt;Order&gt; getOrderList() {
  
          List&lt;Order&gt; orders = orderDao.getAllOrders();
  
          return orders;
      }
  
      public void addNewOrder(Order order) {
  
          orderDao.addOrder(order);
      }
  }
  </code></pre>
  
  <p>The OrderService calls the OrderDao:</p>
  
  <pre><code>@Repository
  public class OrderDao extends JdbcDaoSupport {
  
      @Autowired
      public OrderDao(DataSource dataSource) {
          setDataSource(dataSource);
      }
  
      public List&lt;Order&gt; getAllOrders() {
  
          String sqlQuery = "SELECT id, name, status, orderNumber FROM orders";
  
          List&lt;Order&gt; orders = getJdbcTemplate().query(sqlQuery, new OrderRowMapper());
  
          return orders;
  
      }
  
      public int addOrder(Order order) {
  
          String sqlQuery = "INSERT INTO orders (name, status) VALUES (?, ?)";
  
          getJdbcTemplate().update(sqlQuery, new Object[] { order.getName(), order.getStatus() });
  
          return getJdbcTemplate().queryForObject("SELECT LAST_INSERT_ID()", Integer.class );
      }
  }
  </code></pre>
  
  <p>The Order model looks like:</p>
  
  <pre><code>public class Order {
  
      private int orderId;
      private String name;
      private String status;
      private String orderNumber;
  
      // getters and setters etc.
  }
  </code></pre>
  
  <p>At present my OrderDao only communicates with the database to perform CRUD actions on the Order model. I am not sure whether I should create a placeOrder() method within the OrderDao as this would mean I have a single DAO that accesses both database and SOAP service, which feels wrong.</p>
  
  <p>It also feels wrong to put placeOrder() in the OrderService because the service will contain a mixture of internal DAO calls and external third-party SOAP calls.</p>
  
  <p>I've been reading up on interfaces but I don't think they help me here as my database DAO would contain create(), update(), delete() which wouldn't apply to a SOAP DAO.</p>
  
  <p>Should I just create two DAOs: OrderDaoDatabase and OrderDaoSoap?</p>