什么是 IoC/“Springy”?如何在 GWT 中处理 MVP? (提示,可能不是 Spring Roo 1.1 方式)

发布于 2024-10-09 16:53:15 字数 1126 浏览 0 评论 0 原文

这是 Spring Roo 1.1 制作返回 GWT 活动的工厂的方法(是的,Spring 框架)

 public Activity getActivity(ProxyPlace place) {
    switch (place.getOperation()) {
      case DETAILS:
        return new EmployeeDetailsActivity((EntityProxyId<EmployeeProxy>)place.getProxyId(), requests, 
          placeController, ScaffoldApp.isMobile() ? EmployeeMobileDetailsView.instance() : EmployeeDetailsView.instance());

      case EDIT:
        return makeEditActivity(place);

      case CREATE:
        return makeCreateActivity();
    }

    throw new IllegalArgumentException("Unknown operation "
        + place.getOperation());
  }

在我看来,如果我们使用带有常量的 switch case 来执行操作,我们就回到了几百年前。做一个工厂。现在这是官方自动生成的带有 GWT / GAE 集成的 Spring roo 1.1,我没有骗你,

我只能假设这是一些高管空洞的公告,因为这绝对不是 Spring

似乎 VMWare 和 Google 太快了,以至于没有推出任何东西,并且没有'还没有完全完成,不是吗?

我是否遗漏了一些东西,或者这是半成品,到目前为止不是 Spring + GWT MVP 应该工作的方式?

您是否有更好的示例来说明 Spring、GWT(2.1 MVP 方法)和 GAE 应如何连接? 我不想做所有像这样管理历史和活动的工作。 (没有注释?IOC?)

我也不想重新发明轮子并编写自己的 Spring 增强功能,只是为了发现其他人做了同样的事情,或者更糟糕的是,发现 SpringSource 和 Google 很快就会发布 roo 1.2 并使其正确

This is the Spring Roo 1.1 way of doing a factory that returns a GWT Activity (Yes, Spring Framework)

 public Activity getActivity(ProxyPlace place) {
    switch (place.getOperation()) {
      case DETAILS:
        return new EmployeeDetailsActivity((EntityProxyId<EmployeeProxy>)place.getProxyId(), requests, 
          placeController, ScaffoldApp.isMobile() ? EmployeeMobileDetailsView.instance() : EmployeeDetailsView.instance());

      case EDIT:
        return makeEditActivity(place);

      case CREATE:
        return makeCreateActivity();
    }

    throw new IllegalArgumentException("Unknown operation "
        + place.getOperation());
  }

It seems to me that we just went back hundred of years if we use a switch case with constants to make a factory. Now this is official auto generated Spring roo 1.1 with GWT / GAE integration, I kid you not

I can only assume this is some executives empty announcements because this is definitly not Spring

It seems VMWare and Google were too fast to get something out and didn't quite finish it, isn't it?

Am I missing something or this is half baked and by far not the way Spring + GWT MVP should work?

Do you have a better example of how Spring, GWT (2.1 MVP approach) and GAE should connect?
I would hate to do all the plumbing of managing history and activities like this. (no annotations? IOC?)

I also would hate to reinvent the wheel and write my own Spring enhancement just to find someone else did the same, or worse, find out that SpringSource and Google will release roo 1.2 soon and make it right

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生百味 2024-10-16 16:53:15

我不太用Roo,所以我只能猜测。您引用的代码看起来基本上类似于 "GWT MVP 中示例应用程序中的相应部分使用 Activity 和 Places 进行开发” 文档页面(注意:我没有插入注释,它们来自示例代码)

public class AppActivityMapper implements ActivityMapper {
...

 /**
  * Map each Place to its corresponding Activity. This would be a great use
  * for GIN.
  */
 @Override
 public Activity getActivity(Place place) {
    // This is begging for GIN
    if (place instanceof HelloPlace)
        return new HelloActivity((HelloPlace) place, clientFactory);
    else if (place instanceof GoodbyePlace)
        return new GoodbyeActivity((GoodbyePlace) place, clientFactory);

    return null;
 }
}

如果“switch +使用“getOperation()”,或“if + instanceof”。问题是,该 Activity 是使用 new 运算符构建的。这与依赖注入的基础知识相冲突。

因此,使用 DI 要做的基本事情就是将工厂注入到 ActivityMapper 中,每个类型对应一个 Place:

 public Activity getActivity(Place place) {
    if (place instanceof HelloPlace)
        return helloActivityFactory.build((HelloPlace) place);
    else if (place instanceof GoodbyePlace)
        return goodbyeActivityFactory.build((GoodbyePlace) place);

    return null;
 }

这仍然具有切换特性。这是因为某些东西必须在地点和活动之间进行映射(唯一的选择是将活动工厂注入每个地点)。您可以像这样配置映射:

 Map<String, ActivityFactory> activityMap; /* injected */

 public Activity getActivity(Place place) {
    ActivityFactory factory = activityMap.get(place.getOperation());
    if (factory != null)
        return factory.build(place);

    return null;
 }

...如果可以定义一个使用通用参数的通用 ActivityFactory 接口(例如,在我的草案中,build() 只会使用 place 作为参数)。

现在这是我的猜测(只是猜测!),为什么这样的东西不在 Roo 生成的代码中: “使用活动和场所进行 GWT MVP 开发” 文章是 《大规模应用开发与MVP》,提出了无DI的MVP,并承诺未来会发表一篇《使用Gin减少代码,方便测试》的文章。它仍然不存在,并且示例代码尚未更新以集成依赖项注入。我认为 Roo 生成的代码在某种程度上基于该文章,因为它是该主题的参考。

I don't use Roo very much, so I can only guess. The code you cited looks basically like the corresponding part in the sample app from the "GWT MVP Development with Activities and Places" documentation page (Note: I didn't insert the comments, they're from the example code):

public class AppActivityMapper implements ActivityMapper {
...

 /**
  * Map each Place to its corresponding Activity. This would be a great use
  * for GIN.
  */
 @Override
 public Activity getActivity(Place place) {
    // This is begging for GIN
    if (place instanceof HelloPlace)
        return new HelloActivity((HelloPlace) place, clientFactory);
    else if (place instanceof GoodbyePlace)
        return new GoodbyeActivity((GoodbyePlace) place, clientFactory);

    return null;
 }
}

It doesn't matter much, if "switch + getOperation()" is used, or "if + instanceof". The problem is, that the Activity is built with a new operator. That conflicts with Dependency Injection basics.

So the basic thing to do with DI, is to inject factories into the ActivityMapper, one for each type of Place:

 public Activity getActivity(Place place) {
    if (place instanceof HelloPlace)
        return helloActivityFactory.build((HelloPlace) place);
    else if (place instanceof GoodbyePlace)
        return goodbyeActivityFactory.build((GoodbyePlace) place);

    return null;
 }

This still has the switching characteristic. That's because something must map between Places and Activities (the only alternative would be to inject an activity factory into each place). You could make the mapping configurable like this:

 Map<String, ActivityFactory> activityMap; /* injected */

 public Activity getActivity(Place place) {
    ActivityFactory factory = activityMap.get(place.getOperation());
    if (factory != null)
        return factory.build(place);

    return null;
 }

... if it's possible to define a generic ActivityFactory interface that works with common parameters (e.g. in my draft, build() would only use the place as an argument).

Now here's my guess (just a guess!), why something like this isn't in the Roo generated code: The "GWT MVP Development with Activities and Places" article is a follow-up to "Large scale application development and MVP", which presented MVP without DI, and promised to deliver a future "Using Gin to reduce code and facilitate testing" article. It's still not there, and the example code hasn't been updated to integrate Dependency Injection. I assume, that the Roo generated code is based to some degree on that article, because it's the reference on this topic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文