2

我更好奇这是否是最佳实践或应该避免。假设我有两个 EJBS,每个都使用不同类型的对象,一个小部件和一个 foo。管理小部件的 EJB 需要获取 foo 的集合来进行一些处理。此功能已在 FooManager bean 中创建。

WidgetManager 中的方法正在创建,而 FooManager 方法已经创建。

这是我的意思的一个例子。这是一个简短的例子。

@Stateless
public class FooManager implements FooManagerLocal, FooManagerRemote
{
   public List<Foo> getAllFoosForAWidget(widgetId)
   {
      //runs queries and builds foo list 
   }

   public Boolean isWidgetCloseable(widgetId)
   {
     //a widget is closeable if all foos for that widget are set to "done"

     List<Foo> foos = getallFoosForAWidget(widgetId);
     boolean isCloseable = false;
     //process foos and update isCloseable respectively
     return new Boolean(boolean);

   }

}

@Stateless
public class WidgetManager implements WidgetManagerLocal, WidgetManagerRemote
{
   public void closeWidgetIfFoosAreDone(widgetId) //needs to do stuff with foos
   { 
      //generate the widget based on widgetId
      Widget widget = find(Widget.class, widgetId) 


      //is this appropriate?
      //beans are gathered through our own beanclient
      FooManager fooManager = BeanClient.getBean(FooManager.class);
      if(fooManager.isWidgetCloseable(widgetId)
      {
         widget.setStatus(Close);
         save(widget); //save widget back to database
      }
   }
}

我的问题是 WidgetManager bean 是否应该调用 FooManager bean 中已经创建的方法?客户端是否应该检查哪些小部件可以关闭,然后将 Id 列表发送到 WidgetManager?

我倾向于后一种情况,以便 EJB 不必担心调用彼此的方法。客户端可以使用它们并且只需要发送一个 id 列表。

4

2 回答 2

3

我会在 WidgetManager 中留下简单的“关闭”方法。这样,与 foo 的关系就不会那么紧密了。

因此,只需在其他一些业务 Bean 中,您将注入这两个 bean 并构建您的逻辑。

@Stateless
class SomeBusinessProcess implements ISomeBusinessProcess
{
   @EJB FooManagerLocal fooManager;
   @EJB WidgetManagerLocal widgetManager;

   public void process(WidgetId id)
   {
      if (fooManager.isClosable(id))
            widgetManager.close(id);
   }
}

这样,您将拥有更多自由,并且您的 Manager bean 将更加清晰。

于 2009-07-08T19:42:13.660 回答
1

上面的模式被称为服务门面,在这里进行了很好的讨论:http: //www.adam-bien.com/roller/abien/entry/why_service_isn_t_a

于 2009-07-21T11:02:53.017 回答