3 Replies Latest reply on Oct 1, 2009 8:34 AM by Wolfgang Knauf

    Design: When the line between domain objects and service obj

    Tolga Tarhan Newbie

      I have a design question in the fight about domain driven design, the anemic domain model, and the way EJB/JPA force us to do things.

      Consider a web-mail application which is built using JPA for persistence and EJB for the services layer. Let's say we have a service method in our EJB like this:

      public void incomingMail(String destination, Message message) {
       Mailbox mb = findMailBox(destination); // who cares how this works

      This is seemingly a reasonable business method. Presumably, the Mailbox object will still be attached and it will seamlessly save the changes back to the database. After all, that is the promise of transparent persistence.

      The Mailbox object would have this method:

      public void addMessage(Message message) {

      Here's where it gets complicated -- assume we want to have other mailbox types. Say we have an AutoRespondingMailbox which automatically responds to the sender, and a HelpDeskMailbox which automatically opens a helpdesk ticket with each email received.

      The natural thing to do would be to extend Mailbox, where AutoRespondingMailbox has this method:

      public void addMessage(Message message) {
       String response = getAutoResponse();
       // do something magic here to send the response automatically

      The problem is that our Maibox object and it's subclasses are "domain objects" (and in this example, also JPA entities). The Hibernate guys (and many others) preach a non-dependent domain model -- that is, a domain model that does not depend on container/runtime provided services. The issue with such a model is that the AutoRespndingMailbox.addMessage() method cannot send an email because it can't access, for example, JavaMail.

      The exact same issue would occur with HelpDeskMailbox, as it could not access WebServices or JNDI injection to communicate with the HelpDesk system.

      So you're forced to put this functionality in the service layer, like this:

      public void incomingMail(String destination, Message message) {
       Mailbox mb = findMailBox(destination); // who cares how this works
       if (mb instanceof AutoRespondingMailbox) {
       String response = ((AutoRespondingMailbox)mb).getAutoResponse();
       // now we can access the container services to send the mail
       } else if (mb instanceof HelpDeskMailbox) {
       // ...
       } else {

      Having to use instanceof in that way is the first sign of a problem. Having to modify this service class each time you want to subclass Mailbox is another sign of a problem.

      Does anyone have best practices for how these situations are handled? Some would say that the Mailbox object should have access to the container services, and this can be done with some fudging, but it's definitely fighting the intended usage of JPA to do that, as the container provides dependency injection everywhere except in Entities, clearly indicating that this isn't an expected use case.

      So, what are we expected to do instead? Liter-up our service methods and give-up polymorphism? Our objects automatically become relegated to C-style structs and we lose most of the benefit of OO.

      The Hibernate team would say that we should split our business logic between the domain layer and the service layer, putting all of the logic that's not dependent on the container into the domain entities, and putting all the logic that is dependent on the container into the services layer. I can accept that, if someone can give me an example of how to do that without having to completely give-up polymorphism and resorting to instanceof and other such nastiness.

        • 1. Re: Design: When the line between domain objects and service
          Georg Thimm Newbie


          In the case of the mail-box, why not push the functionallity of reacting to an email into the implementation of the mailbox? E.g. change the mailbox.addMessage() method and dynamically set up a reference to the mailbox when you startup the bean.
          In general, you could use an Adapter/Notifier pattern (with the additional advantage of being able to send one message too several mailboxes).

          Am I missing something there?

          Cheers ,

          • 2. Re: Design: When the line between domain objects and service
            Tolga Tarhan Newbie

            Don't read too much into my specific example. I'm trying to illustrate a point. My point is that EJB and JPA force us into a model where our domain objects cannot rely on any container provided services. I fully understand the reasoning behind it.

            However, I'm asking for ideas on elegant ways to deal with this issue. If certain subclasses of an Entity need to do things that weren't necessarily anticipated when the service layer was created, we have to make changes to the service layer to specifically support these things.

            So each time I subclass Mailbox, I have to modify a service method somewhere to deal with that new type of Mailbox, if that new type has any different behavior which may need a container service. This means we completely miss-out on the polymorphism of the Mailbox object.

            Seems to go against the whole notion of objects, which combine data and behavior, and more into a combination of data structures and procedural methods.

            So, does anyone have any solutions?

            • 3. Re: Design: When the line between domain objects and service
              Wolfgang Knauf Master


              I think you need a layer beetween MailBox and your EJB, which encapsulates the logic specific to the mailbox implementation. "findMailbox" should not return the entity, but the the implementation of the layer.

              Assume an interface (or an abstract base class):

              public interface IMailBoxHandler
               public void handleMail (Message message);

              It has only one method which handles an incoming mail.

              Now you create subclasses/implementations:
              public class MailBoxHandlerHelpDesk implements IMailBoxHandler
               public void handleMail (Message message)

              and so on...

              "findMailBox" should probably return an appropriate IMailBoxHandler implementation.

              "incomingMail" would be:
              public void incomingMail(String destination, Message message) {
               IMailboxHandler mb = findMailBox(destination);
               mb.handleMail (message);

              This way, your domain model is free of controller logic.

              Hope this is a reasonable approach ;-)