Skip to content

‘Disappeared’ DAO layers

Adam Bien still wants to believe that a JPA layer can directly replace a formal ‘DAO layer’. And I still disagree.

I might agree that for simple enough systems, it could end up being that in fact the service layers end up with PersistenceManager logic directly in their methods. But then I might say a language and framework like Ruby on Rails could suit your application better in that case. For any sufficiently complex Java-based enterprise system, a POJO-style data persistence layer naturally evolves, in order to reduce as far as possible the responsibilities the service layer must carry out.

As mentioned by commentators on his blog, the discipline of test-driven design is usually  a strong driver of having a separated set of components that deal with persistence and other data concerns. Not just from the issue over mocking the PersistenceManager, which is certainly a strong driver, but also from design principles.

Service layers marshall requests from clients, and decompose them to call various component layers. Data access layers typically form part of these component layers. A TDD discipline means these data access layers usually end up with more than just CRUD operations.

Furthermore, as a matter of discipline, in any sufficiently complex service layer I prefer not to work directly with database abstractions (i.e. entities that are modelled on database tables and their relationships) but on first-class objects designed to fit directly with the modelled domain. I want those objects to have rich behaviour – the service layer is actually part of their behaviour. I want to start the design of the system by directly modelling those actions, e.g.

passenger.at(origin);
passenger.orderJourney(dispatch, destination);
Vehicle vehicle = dispatch.sendVehicle(origin);
vehicle.pickUp(passenger);
vehicle.arrives(destination).dropsOff(passenger);
vehicle.notify(dispatch);

I do not want these objects to know about database semantics. That is a responsibility of a separated persistence layer, which we may for convienience sake, called the DAO. Even if internally we end up with;

class Passenger {
  at(Location origin) {
    this.origin=origin;
  }
  call(DispatchCentre dispatch, Location destination) {
    dispatch.passengerOrder(this, destination);
  }
}

DispatchCentre at this level, is a service, but we will model it as part of the domain of passengers, locations, and vehicles. Behind that service, it will have a database layer with the semantics as to how it saves and loads the data concerning passengers, locations, and vehicles. I don’t necessarily want the client objects (e.g. the web action) to have to deal with JPA annotated objects. They just deal with objects as much as possible. And I don’t see there’s anything served with developing possibly complex persistence logic directly in the DispatchCentre. In fact, taking a top-down approach, I know pretty much I won’t do that.

Only after I’ve done this initial part of the modelling, will I be concerned as to which of these components can be directly JPA or Hibernate annotated, which are Spring-injected services, which are Session EJBs, and the like. I think starting your modelling from the point of view of having a persistence framework like JPA or Session EJBs or any other ‘archtectural artifact’ leads to an unnatural design.

11 Comments

  1. Adam Bien wrote:

    “I might agree that for simple enough systems, it could end up being that in fact the service layers end up with PersistenceManager logic directly in their methods. But then I might say a language and framework like Ruby on Rails could suit your application better in that case.”

    Does it mean: all Rails applications are either simple or not maintainable/testable?

    regards,

    adam

    Friday, February 6, 2009 at 21:43 | Permalink
  2. Scot Mcphee wrote:

    “Does it mean: all Rails applications are either simple or not maintainable/testable”

    Certain not! Rails apps are very testable. I just mean they are suited more simple architectures than Java web apps.

    What I mean overall, and what I probably should have just said, is that as developers we should follow are standard set of practices and disciplines and build our apps with TDD, programming-by-intent and so forth until we are finished. I find When I use these methods that ‘infrastructure’ issues (such as where JPA fits) tends to recede into the background. I don’t like putting persistence methods on my service classes, otherwise the service class becomes very quickly cluttered. So a natural ‘DAO’ object layer emerges from this. I also have a strong prejudice against JPA or hibernate annotated objects ‘escaping’ the persistence layer. I find it disconcerting that the client layer would require a dependency on persistence infrastructure.

    Saturday, February 7, 2009 at 07:37 | Permalink
  3. Sakuraba wrote:

    Not having persistence logic abstracted => Mixing different concerns in one big service => makes it hard to test the service, because the databse abstraction must be there and cannot be simply mocked away => as the system grows applying this pattern leads to unmaintainable and untestable messy code that will drive you into unhappiness, kill your productivity and will make the project fail

    Search for “Misko Hevery” (a Test Engineer at Google) on Google Video to have your eyes opened. Those videos made me a TDD-guy, because I finally saw what was wrong with my code structuring.

    No being able to replace an implementation of a specific concern e.g. by using DI and interface-driven-design, with a ‘seam’/mock/test-spy is the dead end for testability. This is exactly what happens when you use EntityManager inside your services. Mixing Services with EntityManager-data-access leads to untestable code. No excuse, end of story.

    Sunday, February 8, 2009 at 23:29 | Permalink
  4. Dimitris Menounos wrote:

    “I also have a strong prejudice against JPA or hibernate annotated objects ‘escaping’ the persistence layer.”

    If that was about DTOs I will have to disagree. I generaly avoid them like the plague and always strive to use my POJO Entities as much as possible.

    “I find it disconcerting that the client layer would require a dependency on persistence infrastructure.”

    Would you care to elaborate on that? As I said I use entities all the way through the client and have never had such a problem.

    Monday, February 9, 2009 at 19:04 | Permalink
  5. Scot Mcphee wrote:

    Hello Dimitris

    Thanks for your excellent comment.

    I think it’s a mistake that any client of a service layer has to also import, for example, org.hibernate.* – javax.persistence is a little bit less pernicious than hibernate, but only slightly. I really don’t like such persistence artifacts escaping the layers that are directly dealing with them. Why should a Struts action or a Tapestry page class (etc) be concerned about hibernate or any other database semantic? The point of layering is to isolate concerns. So for me, this means, if you want domain classes to also be your persistence model, no annotations (generally speaking). Cfg files I can live with, and in fact, over the past while I have increasingly leaned towards using e.g. .xbm configuration to map the domain classes to the database and have moved very much away from annotations to do it (this is also to do with what I see as the poor readability of the Java 5 extensions).

    I also think, that it’s a huge mistake to assume that a domain model will exactly match the most performant or otherwise optimum relational model of the data. There is a fundamental mismatch – sometimes (and sometimes it’s not, in which case I’ll lean to the solution above). Also sometimes, you might have a relational persistence model forced on you by an already-existing database. To just assume, that the model that you can use to describe the functioning of the system (as for example illustrated in my rather twee taxi-ordering example above) is the same one as storage of the data is a big mistake (again, sometimes, not always). I find, as you develop a system, driven by its domain description, you want to be able to rapidly evolve the domain model in ways that may be incompatible with the slower evolution of a database structure and it’s normalised form (or for example, its highly optimised and specialised, relational form – especially if the database is already ‘legacy’ and can’t be changed as easily as the OO domain model).

    Monday, February 9, 2009 at 19:53 | Permalink
  6. Dimitris Menounos wrote:

    I use xml mappings in place of annotations too, but mostly because of preference. I can’t see how using annotations would make the client import javax.persistence stuff though.

    For your next point, I haven’t been so far imposed a legacy schema to work on, yet still I think that that is exactly the value ORMs should be providing. The mapping of the database relational data to the actual application model. If a whole another layer of classes, in addition to the metadata, has to fit between the ORM and the model then IMHO something is not very right.

    To make myself clear, I don’t say that I always use pure entities in the client. It depends on the use case. For example if I had to display a complex grid of entities along with some of their relations and other computed details, I would probrably use a wrapper object (DTO if you will)..

    Monday, February 9, 2009 at 22:08 | Permalink
  7. Good post!

    Allthough the debate about DAOs is at least in its third round nowadays, it still seems to riling up a good part of developers minds.

    For myself, i posted an answer to Adam’s very first entry on that subject but still wait for an answer … ;o)

    You might want to take a look at

    http://gleichmann.wordpress.com/2007/11/22/why-dao-wont-die/

    Greetings

    Mario

    Monday, February 9, 2009 at 23:23 | Permalink
  8. Scot Mcphee wrote:

    Oh, the annotations are in the relevant namespace, e.g. the @Entity annotation is javax.persistence.Entity ( see http://java.sun.com/javaee/5/docs/api/javax/persistence/Entity.html ). So once you put that on a class all its clients have a transient dependency to it.

    As I state in the article, I prefer to work (though not always – depends if I’ve got a ‘clean slate’ or not among other things) by modeling the target domain first in a TDD fashion. The main reason is that you can usually actually model (i.e. test then write) a surprising amount of behavior in the system before you even have to consider an actual functioning persistence mechanism!

    I guess that’s maybe another reason why I think declaring the-service-does-the-persistence to be the wrong idea – it’s putting the persistence methods ahead of the business problem domain.

    Monday, February 9, 2009 at 23:31 | Permalink
  9. Scot Mcphee wrote:

    Mario, that’s an excellent post, I see written over a year ago, and I agree with all the points in it!

    Monday, February 9, 2009 at 23:37 | Permalink
  10. Dimitris Menounos wrote:

    “Oh, the annotations are in the relevant namespace… So once you put that on a class all its clients have a transient dependency to it.”

    Forgive me, I still don’t understand :) For example:

    package foo;
    public @interface Bar {
    // custom annotation
    }

    package model;
    @foo.Bar
    public class Person {
    // model class with annotation
    }

    public class Test {
    public static void main(String[] args) {
    new model.Person();
    }
    }

    Test (the client) needs not foo.Bar in order to compile and run.

    Tuesday, February 10, 2009 at 01:03 | Permalink
  11. Scot Mcphee wrote:

    Yes it does – to deserialize it. And if you’re using maven, the dependency is brought in transiently.

    Tuesday, February 10, 2009 at 07:27 | Permalink

One Trackback/Pingback

  1. [...] February 17, 2009 — Mario Gleichmann Again there were some interesting debates in the near past, which once again discussed the role of the DAO within enterprise applications, [...]