2009-10-08 21 views
1

我一直在研究Seam框架很久。儘管我不在工作中使用它,但我喜歡它的方法。這很有趣。但我有些懷疑。在閱讀Seam in Action書籍後,我認爲你不可能將多於一個參數綁定到商業方法。類似於在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上下文

//

@Stateless 
public class BusinessObjectImpl implements BusinessObject { 

    public void doSomething() { 

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

     SomeObject output ...    

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

} 

如果第一種方法在無狀態中使用,它有很多方法,所以我認爲最好使用Command模式爲業務對象建模。我對嗎 ?喜歡的東西

public class DoSomething implements Command { 

    @In 
    private SomeObject input01; 

    @In 
    private SomeObject input02; 

    @Out 
    private SomeObject output01; 

    public void execute() { 

    } 

} 

你:什麼模式(和良好做法)你使用,以避免在一個無狀態的業務對象的許多成員字段?

問候,

回答

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);  
    } 

} 

EDIT(基於評論):

有從使用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 
    } 
} 

但這種方法是不完全因爲它依賴SecondBean和ThirdBean在您當前的會話/對話中。你可能會更好地遵循擁有一個頁面Controller或Backing Bean的方法。這可以是一個POJO,然後稱之爲您的SLSB。例如:

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

和背豆:

@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); 
    } 
} 

這開始看起來很像你原來的問題;-)

+0

謝謝你,大摩。我如何將一個以上的參數從JSF頁面傳遞給業務對象?你能告訴我如何...?我是Seam的新手。 – 2009-10-08 17:06:45

+0

謝謝,真的很好,我的下一步就是成爲Seam的開發者。 – 2009-10-08 18:55:23