简体   繁体   English

如何在GWT MVP体系结构中处理服务和事件总线实例?

[英]How to handle service and eventbus instances in GWT MVP Architecture?

We are developing an app using the MVP pattern, as described in this guide: 如本指南所述,我们正在使用MVP模式开发应用程序:

http://code.google.com/webtoolkit/articles/mvp-architecture.html http://code.google.com/webtoolkit/articles/mvp-architecture.html

When creating the controller instance we do the following: 创建控制器实例时,请执行以下操作:

appController = new AppController(service, eventBus);
appController.go(RootPanel.get("SOME_SLOT"));

Now, when the controller creates a certain presenter, it does something like this: 现在,当控制器创建某个演示者时,它会执行以下操作:

sthPresenter = new SthPresenter(service, eventBus, new SthView());
sthPresenter.go();

The presenter than saves the eventBus and the service to a private field variable, and uses either as needed. 演示者随后将eventBus和服务保存到私有字段变量,并根据需要使用其中之一。

As the application grows, we have more and more presenters and views, so the question is can we use a different method of obtaining the service and the eventBus in the presenters, without passing a reference via the constructor of each presenter. 随着应用程序的增长,我们有越来越多的演示者和视图,所以问题是我们可以使用不同的方法来获取演示者中的服务和eventBus,而无需通过每个演示者的构造函数传递引用。

For example, creating a static field in the controller and just calling it with something like AppController.getService(). 例如,在控制器中创建一个静态字段,然后仅使用AppController.getService()之类的方法调用它。 Maybe a singleton pattern. 也许是单例模式。

Would a static field in the controller (or somewhere else) be a bad idea for this design. 对于这种设计,控制器(或其他位置)中的静态磁场会是一个坏主意吗? Keep in mind that the code is compiled to javascript, if that makes any difference. 请记住,如果有任何区别,代码将被编译为javascript。

I'd highly suggest dependency injection (DI). 我强烈建议依赖注入 (DI)。 It allows you to avoid boilerplate code (singletons, etc.), global state and in general leads to a more testable code. 它使您可以避免样板代码(单例等),全局状态,并且通常会导致可测试的代码。 Misko Hevery has some very interesting posts, including the very informative guide to writing testable code . Misko Hevery有一些非常有趣的帖子,包括非常有用的编写可测试代码的指南

For DI in GWT you should use Gin - a wrapper around the popular Guice DI framework . 对于GWT中的DI,您应该使用Gin-流行的Guice DI框架周围的包装器。 I've been using it for a rather complex project and just using DI/Gin (and thinking how it should be applied most effectively) has definitely lead to a more "clean", testable code. 我一直在将它用于一个相当复杂的项目,仅使用DI / Gin(并思考如何最有效地应用它)肯定会导致更“干净”的,可测试的代码。

Use a singleton and an Observer pattern. 使用单例和观察者模式。 Make sure you only use high level events for your notifications, if not you will end up with a nightmare. 确保只对通知使用高级事件,否则,将导致一场噩梦。

The code compiling to JavaScript is really transparent to you. 编译为JavaScript的代码对您来说实际上是透明的。

This is a situation very often encountered in GWT MVP applications. 这是GWT MVP应用程序中经常遇到的情况。 In my application, I use dependency injection (with GIN ) to inject the event bus into presenters. 在我的应用程序中,我使用依赖项注入(与GIN一起 )将事件总线注入到演示者中。 Presenters themselves are singletons and can be eagerly instantiated if needed. 演示者本身就是单例,可以根据需要实例化。 Doing so, however, will greatly reduce the scalability of your application since a large app will require you to instantiate many presenters as soon as it starts up. 但是,这样做会大大降低应用程序的可伸缩性,因为大型应用程序需要您在启动后立即实例化许多演示者。

Correctly solving this problem yourself can be a bit involved. 您自己可能需要正确解决此问题。 I suggest you take a look at the GWT-platform framework , which handles many of the more difficult problems associated with a GWT MVP app, including lazy instantiation of presenters and views, history management, efficient code splitting, etc. 我建议您看一下GWT平台框架 ,该框架处理了与GWT MVP应用程序相关的许多更困难的问题,包括演示者和视图的延迟实例化,历史记录管理,有效的代码拆分等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM