Which one is better when and why?

Discussions

EJB design: Which one is better when and why?

  1. Which one is better when and why? (4 messages)

    I have two design options:

    1) contact.setAddress("new address");
       contact.update();

    2) contact.setAddress("new address");
       contactManager.updateContact(contact);

    In first option, business object Contact is having responsibility to update itself but in second option, ContactManager is called to update Contact.

    Which option is better when and why?

    Threaded Messages (4)

  2. updateContact is another aspect[ Go to top ]

    When you did contact.setAddress you already updated contact.

    So when you do contactManager.updateContact or contact.update you are doing another aspect/concept than updating contact, you are going modify something else, probably a database. Why contact should have anything to do with the database? Ideally the call to contactManager.updateContact should be transparent. Ideally the method himself (updateContact) should not need to exist. JDO and hibernate provide this transparency under conditions.
  3. transparency[ Go to top ]

    Thanks for the response Sebastien.

    Your answer seems logical but let's suppose change of address is happening at another layer, say web layer through form, so now you are left with task of only updating contact in database. Here you can call object to update itself or pass it to some manager so manager is updating the passed object in database (using EJB, or PersistenceManager in JDO, or Session in hibernate). I can provide update method in Contact object which will take necessary actions to persist changes and ContactManager can be transparent to user. Or as an alternative I do not put update method in Contact and provide ContactManager and user has to call this manager for updating contact.

    In lot of codebase I see Manager and at very few place update method in business object itself, and I was wondering why!
  4. Which one is better when and why?[ Go to top ]

    In first option, business object Contact is having responsibility to update itself but in second option, ContactManager is called to update Contact.Which option is better when and why?

    As the previous poster said, there is a difference between updating a POJO (contact), and having changes to an object be reflected in a database or some other external store.
    You hit the nail on the head though with a rather valid question - why doesn't a Contact know how to update itself? Why can't the call to setAddress() persist automatically to whereever it should?
    There are several answers....
    As the previous reply says, there are several frameworks that actually do what you're asking about. When I asked this question I was directed to look at BMP (Bean Managed Persistence), where each object knows how to update itself in the database.... what you can rather quickly end up with is a nice mess :)

    By using a "Manager" or some other facade to handle the interactions between persisting your model objects and the actual persistence framework you can at least get closer to centralizing this persistence... It also allows centralization of validation rules, object interactions that may need to be triggered as a result of an address change (should setAddress() fire an email to notify someone of an address change?), logging and auditing, security (can the currently executing user actually set the address?)...... It is a bit of a pain to get used to though, as you are taught (hopefully) from day one that Objects have not only state but behavior, and using a manager or facade seems REALLY procedural... but it does simplify things. I hope this hasn't been a meaningless ramble.

    Keep questioning things. It is good to ask why.
  5. Which one is better?[ Go to top ]

    I agree with Brian's point that "Manager" is to centralize persistence and other related operations, and I am not against the use of such manager but the question is whether to expose manager to client class or not. Let me explain with code:

    Option:1
    ---------------------------
    Client.java {
    ....
    contact.setAddress("new address");
    contact.update();
    ...
    }

    Contact.java {
    ......
      update() {
        contactManager.updateContact(this);
      }
    .....
    }

    ----------------------------------


    Option:2
    -----------------------------------
    Client.java {
    ....
    contact.setAddress("new address");
    contactManager.updateContact(contact);
    ...
    }

    Contact.java {
    ......
      // no update() method
    .....
    }
    ----------------------------------


    In both options there is contactManager but in option 1, Client.java need not know about it whereas in option 2, Client.java must call contactManager to update the contact.

    I hope I am clear enough.