在Seam框架中建模业务对象的模式?

时间:2009-10-08 12:23:35

标签: java design-patterns jsf java-ee seam

我一直在研究Seam框架。尽管我不在工作中使用它,我喜欢它的方法。这很有趣。但我有些疑惑。阅读行动书中的Seam后,我认为您不可能将多个参数绑定到业务方法。像

这样的东西
@Stateless
public class BusinessObjectImpl implements BusinessObject {

    public void doSomething(SomeObject i01, SomeObject i02 and so on...) {


    }

}

我是对的吗?因此,您有两种方法:

  • @In(注射用)和@Out(用于注射)

//

 @Stateless
 public class BusinessObjectImpl implements BusinessObject {

    @In
    private SomeObject input01;

    @In
    private SomeObject input02;

    @In
    private SomeObject input03;

    @Out
    private SomeObject output01;

    @Out
    private SomeObject output02;

    @Out
    private SomeObject output03;


    public void doSomething() {
        // some code
    }

}
  • 您可以使用Seam Context

//

@Stateless
public class BusinessObjectImpl implements BusinessObject {

    public void doSomething() {

        SomeObject input = Context.get("contextualName");

        SomeObject output  ...            

        Context.set("contextualName", output);
    }

}

如果第一种方法用于有无多种方法的无状态,那么我认为使用命令模式建模业务对象会更好。我对吗 ?像

这样的东西
public class DoSomething implements Command {

    @In
    private SomeObject input01;

    @In
    private SomeObject input02;

    @Out
    private SomeObject output01;

    public void execute() {

    }

}

您:您使用什么模式(和良好做法)来避免无状态业务对象中的许多成员字段?

的问候,

1 个答案:

答案 0 :(得分:2)

没有。那是完全错误的。当然,在Seam方法中可以有很多参数。它只是Java。这段代码很好:

@Stateless
public class BusinessObjectImpl implements BusinessObject {

    public void doSomething(SomeObject i01, SomeObject i02) {


    }
}

当然,Seam允许你做的一件事就是注入你可能会觉得有用的任何其他类。也许是这样的:

@Stateless
public class BusinessObjectImpl implements BusinessObject {

    @In
    private AnotherObject anotherObject;

    public void doSomething(SomeObject i01, SomeObject i02) {
       anotherObject.someMethod(i01, i02);    
    }

}

编辑(根据评论):

有一些方法可以使用JBoss EL从页面传递多个参数(允许对象作为参数)。例如

<h:form>
  <h:commandButton action="#{firstBean.performAction(secondBean, thirdBean)}">Go</h:commandButton>
</h:form>

您的会话/对话中已填充SecondBeanThirdBean的位置(及其相关的@Name),您的FirstBean类似于:

@Name("firstBean")
@Stateless
public class FirstBean {
  public void performAction(SecondBean secondBean, ThirdBean thirdBean) {
    //stuff
  }
}

但是这种方法并不完全优雅,因为它依赖于当前Session / Conversation中的SecondBean和ThirdBean。您可能最好采用具有页面控制器或后备Bean的方法。这可以是POJO然后调用您的SLSB。例如:

<h:form>
  <h:commandButton action="#{backingBean.performAction}">Go</h:commandButton>
</h:form>

Backing Bean:

@Name("backingBean")
@Scope(ScopeType.CONVERSATION)
public class BackingBean {
  @In
  private FirstBean firstBean;

  @In
  private SecondBean secondBean;

  @In
  private ThirdBean thirdBean;

  public void performAction() {
    firstBean.performAction(secondBean, thirdBean);
  }
}

这看起来很像你原来的问题; - )