Smart domain objects? or simple DTO objects? or both?

Discussions

EJB design: Smart domain objects? or simple DTO objects? or both?

  1. We're grappling with a tension in our architecture, and we're looking for all feedback on how others deal with this.

    We have our domain model consisting of clients, campaigns, lists, jobs, tasks, etc. We're using Hibernate.

    We want to do two things:

    - We want to add interesting business behavior to these objects, to enrich our domain model and make it easier to implement business logic. (Basically, to convert these objects from plain data beans to intelligent business objects.)

    - We want to pass these objects to the web tier, so the web tier can update them appropriately and then pass them back to the service layer. (Basically, to use these objects as DTOs (Data Transfer Objects).)

    These two goals seem to be in conflict. It's nice that with Hibernate (unlike with entity beans) we *can* pass our domain model objects right out to the web tier, for presentation or basic modification. But the more business methods we add to our domain objects, the more methods they will have that *can't* be safely called from the web tier.

    In other words, DTO-type domain objects want to have simple APIs that just get and set data (that can be safely called from the web tier, followed by passing them back to the service layer for saveOrUpdate); rich business-logic domain objects want to have complex APIs that *cannot* be called from the web tier (only inside the container where there is a database connection and transaction context).

    So what do other people do? Use XDoclet to generate DTOs? Use some basic conventions (naming conventions?) for indicating which methods are and aren't safe to call from the web tier? Use package scope to keep the business logic methods only usable from other business objects?

    All guidance appreciated :-)
    Cheers!
    Rob
  2. Hi
    Try to put the complex API methods in objects that extend the baisc domain objects. So the web tier will work only with the simple API and the service layer will the extended complex API also. And because you are using Hibernante I don't think the new level in the domain object class hierarchy will pose any major persistence problem.
    Best regards, Mircea