This discussion is archived
4 Replies Latest reply: Apr 27, 2009 8:52 AM by 807588 RSS

Passing an EJB around.  Bad idea?

807588 Newbie
Currently Being Moderated
Hi guys.

I looked around a bit, but I couldn't find anything that was very helpful. I think I've come up with an idea that is either really smart, or really dumb. I have two different entities, but they have one field in common. Whenever that field changes on either of the entities, there is some logic that has to be executed. A part of the logic will be common to both beans, but each entity will also have a unique piece of logic associated with it. Each one of those entities has a bean associated with it, and that bean contains methods with the above logic. I thought about encapsulating the common logic in a third bean, then passing having the other two beans call that. Now, what I'm wondering about is whether it is a good idea to have the first two EJBs implement a common interface defining the methods that should be executed when the given field changes, then pass the EJBs (which implement the common interface) as a parameter, execute the common logic, and then call the methods defined by the interface from the third bean. I could write the logic in both beans, but I want to try and avoid duplication of code. I could also pass the entities to the method on the third EJB instead of the EJB associated with the entity and put all the logic in the third EJB, but that would mean having an if statement in the method that checks the class of the entity parameter, which also feels a bit dirty. I like having methods that have only one purpose; instanceof checks all over the place look nasty. The only other option that seems viable would be to give the first two beans a common superclass which has abstract methods for the logic and calls the method on the third bean.

I know this is a rather bad explanation, but hopefully a piece of code will get it across. I want to do this, but with EJBs.
interface CommonIF {
    public void doStuff();
    public void doMoreStuff();
}

class BeanOne implements CommonIF{

    private BeanThree beanThree = new BeanThree();

    public void someMethod() {
        beanThree.doCommonStuff(this);
    }

    public void doStuff() {
        //Do more stuff unique to BeanOne
    }

    public void doMoreStuff() {
        //Do more stuff unique to BeanOne
    }

}

class BeanTwo implements CommonIF {

    private BeanThree beanThree = new BeanThree();

    public void someMethod() {
        beanThree.doCommonStuff(this);
    }

    public void doStuff() {
        //Do stuff unique to BeanTwo
    }

    public void doMoreStuff() {
         //Do stuff unique to BeanTwo
    }
}

class BeanThree {

    public void doCommonStuff(CommonIF commonIf) {
        //Do common stuff

        if (someCondition) {
            commonIf.doStuff();
        } else {
            commonIf.doMoreStuff();
        }
    }
}
Any help will be much appreciated. Thanks a lot.

Regards,
codingMonkey

P.S. I know the the EJB forum might be a better place, but I don't want to wait forever before I get a reply.
  • 1. Re: Passing an EJB around.  Bad idea?
    800308 Newbie
    Currently Being Moderated
    Rip your logic out of your EJB's a put it into BO's (POJO's)... The EJB's are just thin wrappers, whose only responsibility is context... Each bean delegates all the business-logic to it's BO (or two, or three).... your BO's can of course implement multiple interfaces, and two BO's might have an interface in common.... which might be used by a third BO's

    Might that help?
    but each entity will also have a unique piece of logic
    Hmmmm... That word "entity" worries me... are you talking entity as in individual instance, or as meaning "class".
  • 2. Re: Passing an EJB around.  Bad idea?
    807588 Newbie
    Currently Being Moderated
    corlettk wrote:
    Rip your logic out of your EJB's a put it into BO's (POJO's)... The EJB's are just thin wrappers, whose only responsibility is context... Each bean delegates all the business-logic to it's BO (or two, or three).... your BO's can of course implement multiple interfaces, and two BO's might have an interface in common.... which might be used by a third BO's
    I have to admit, that is the first time that I've heard the term BO. ([http://en.wikipedia.org/wiki/Business_object_(computer_science)] )
    I read the Wikipedia page, and that is pretty much what we use EJB's for, though the BO multiple BO's for each bean idea sounds pretty useful. We do have a lot of interaction between beans though, and I'm not too sure about how that would translate to using POJO's, especially when I need to call methods on another bean.
    Might that help?
    Yeah, thanks a lot Keith.

    >
    but each entity will also have a unique piece of logic
    Hmmmm... That word "entity" worries me... are you talking entity as in individual instance, or as meaning "class".
    Entity as in a class with the @Entity annotation. I apologise, I should've been a bit more specific.

    I should also have mentioned that all of this will happen only on the server. I'm not crazy enough to try and pass an EJB over the network.

    Edited by: codingMonkey on 2009/04/27 04:20
  • 3. Re: Passing an EJB around.  Bad idea?
    807588 Newbie
    Currently Being Moderated
    codingMonkey wrote:
    I have to admit, that is the first time that I've heard the term BO.
    The next phrase is, "but now that I think about it, that sure explains public school."
  • 4. Re: Passing an EJB around.  Bad idea?
    807588 Newbie
    Currently Being Moderated
    If anyone's interested, I settled for a common superclass for my beans, with abstract methods containing the logic. It might not be the best way to do it, but I'm worried that passing EJB references as method arguments might come back and bite me in the [rear end]. Once again, thanks for the help.