在哪里存储“全局” Eclipse RCP应用程序中的数据? [英] Where to store "global" data in Eclipse RCP Application?

查看:139
本文介绍了在哪里存储“全局” Eclipse RCP应用程序中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Eclipse RCP的初学者,我正在尝试为自己制作一个应用程序。我对实际处理模型对象的实际情况感到困惑。没有一个例子可以解决我遇到的问题,所以我怀疑我是错误的方式。

I'm a beginner with Eclipse RCP and I'm trying to build an application for myself to give it a go. I'm confused about how one actually goes about handling model objects. None of the examples I can find deal with the problem I'm having, so I suspect I'm going about it the wrong way.

说我需要初始化具有持有认证用户信息的类的应用程序。我使用我的 WorkbenchWindowAdvisor (错误的地方?)执行一些初始化(例如身份验证)来决定要显示的视图。一旦完成,就会显示一个视图。现在,该视图需要访问我之前检索/生成的用户信息。

Say I need to initialise the application with a class that holds authenticated user info. I used my WorkbenchWindowAdvisor (wrong place?) to perform some initialisation (e.g. authentication) to decide what view to show. Once that's done, a view is shown. Now, that view also needs access to the user info I had earlier retrieved/produced.

问题是,该视图应该如何得到这些数据?视图连接在 plugin.xml 中。我没有看到任何方式可以给出数据的视图。所以我假设视图必须以某种方式检索。但是它从哪个地方检索它是什么?我想把静态变量放在 IApplication 实现中,但是这样做错了。任何建议或指针非常感谢。谢谢。

The question is, how is that view supposed to get that data? The view is wired up in the plugin.xml. I don't see any way I can give the data to the view. So I assume the view has to retrieve it somehow. But what's the proper place for it to retrieve it from? I thought of putting static variables in the IApplication implementation, but that felt wrong. Any advice or pointers much appreciated. Thanks.

推荐答案

您所面临的问题在我看来不是RCP相关的。它更多的是一个建筑问题。您的观点与业务逻辑有关!
解决方案可以通过两种(通用的)设计模式完成:

The problem you are facing here is in my opinion not RCP related. Its more an architectural problem. Your view is wired with business logicand! The solution can be done by two (common) design-patterns:


  1. 模型视图控制器(MVC) li>
  2. Model-View-Presenter(MVP)

您可以在网页上找到有关这方面的大量信息。我将使用MVP为您的特定问题指出一个可能的解决方案。

You can find plenty information about this in the web. I am going to point a possible solution for your particular problem using MVP.

您将需要创建多个项目。一个当然是一个RCP插件,可以称之为 rcp.view 。现在你创建另一个,这不会使UI贡献(只有org.eclipse.core.runtime开始),并调用它 rcp.presenter 。为了简化事情,这个插件也将成为现在的典范。

You will need to create several projects. One is of course an RCP plugin, lets call it rcp.view. Now you create another one, which doesnt make UI contributions (only org.eclipse.core.runtime to start with) and call it rcp.presenter. To simplify things, this plugin will also be the model for now.

下一步:


  1. 将rcp.presenter添加到rcp.view的
    依赖关系(其
    重要,演示者没有
    引用视图) li>
  2. 导出您要
    将在rcp.presenter
    中创建的所有包,以便它们可见

  3. 在rcp.presenter中创建一个界面
    IPerspective ,它有一些方法
    like( showLogiDialog(),showAdministratorViews(User user),showStandardViews(User user)

  4. 在构造函数中创建采用 IPerspective 的类 PerspectivePresenter ,并将其保存在属性

  5. 在rcp.view中转到你的Perspective,实现你的接口IPerspective ,在构造函数中创建一个新的引用 presenter = new PerspectivePresenter(this) cal /
  6. l presenter.load()和引用
    这在演示者可能就是这样

  1. Add the rcp.presenter to the dependencies of rcp.view (its important that the presenter has no reference to the view)
  2. Export all packages that you are going to create in the rcp.presenter so they are visible
  3. In rcp.presenter create an interface IPerspective that has some methods like (showLogiDialog(), showAdministratorViews(User user), showStandardViews(User user))
  4. Create a class PerspectivePresenter that takes IPerspective in the constructor and saves it in an attribute
  5. In rcp.view go to your Perspective, implement your interface IPerspective, and in the constructor create a new reference presenter = new PerspectivePresenter(this)
  6. call presenter.load() and implenent this in the presenter maybe like this

代码:

public void load()
{
  User user = view.showLoginDialog(); // returns a user with the provided name/pw
  user.login(); // login to system/database
  if(user.isAdministrator())
    view.showAdministratorViews(user);
  else
    view.showStandardViews(user);
}

如您所见,该视图只是创建了一个引用者的引用,负责所有的业务逻辑,主持人会告诉我们要显示的内容。所以在您的Perspective中,您可以实现这些接口功能,并且可以在每个视图中以不同的方式设置透视图。

As you can see, the view just creates a reference to the presenter, which is responsible for all the business logic, and the presenter tells the view what to display. So in your Perspective you implement those interface functions and in each one you can set up your Perspective in a different way.

对于每个View,以相同的方式,您将需要一个执行操作的视图的演示者,并告诉视图(使用界面)显示和传递最终数据的内容。视图并不在意逻辑。使用JFace-Databindings时,这也非常有用(那么只有绑定的数据被传递给视图)。
例如,WorkbenchWindowAdisor将只创建应用程序中需要的所有内容。其他视图,视角,然后可以启用/禁用菜单等,具体取决于他们获得的数据(例如当管理员可能要启用特殊的adminMenu时)。

For each View it goes in the same way, you will need a presenter for the view which performs operations and tells the view (using the interface) what to display and passing down the final data. The view doesnt care about the logic. This is also very usefull when using JFace-Databindings (then only bound data is passed to the view). For example, the WorkbenchWindowAdisor will just create everything that is needed in the application. Other views, perspectives, then can enable/disable menus and so on depending on the data they got (like when isAdministrator you might want to enable an special adminMenu).

我知道这是一个相当沉重的方法,但是Eclipse RCP是专为大型应用而设计的。所以你应该花一些时间在正确的架构。我的第一个RCP应用程序就像你描述的那样...我从来不知道在哪里存储东西,以及如何处理所有的引用。在我的工作中,我学到了MVP(而且我还在学习中)。需要一段时间来理解这个概念,但它的价值。

I know this is quite a heavy approach, but the Eclipse RCP is designed for big (as the name says rich) applications. So you should spend some time in the right architecture. My first RCP app was like you described...I never knew where to store things and how to handle all the references. At my work I learned about MVP (and I am still learning). It takes a while to understand the concept but its worth it.

您可能需要查看我在此问题上的第二篇文章,以获得有关如何构建插件的另一个想法。

You might want to look at my second post at this question to get another idea on how you could structure your plugins.

这篇关于在哪里存储“全局” Eclipse RCP应用程序中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆