Our e3/e4 RCP application consists of a bunch of components with model fragments. Before the upgrade to e4 we had Guice modules in each of the components to inject members into the class.
Currently we have set up a superclass to detect custom DoExecute and DoCanExecute annotations that uses the ContextInjectionFactory to invoke these methods with a custom context.
Example:
IEclipseContext subContext = (IEclipseContext) context.get(contextName);
return (T) ContextInjectionFactory.invoke(this, annotation, subContext);
This succeeds in using a specific context to inject members while executing the methods we have created annotations for, but is a little ugly and will have to be done for all annotations that inject in e4.
What is the best way in e4 to define and use a custom context? Note: We do not want to use the IApplicationContext as it would go against the modular setup per component.
The IEclipseContext is managed by the Eclipse4 framework in a hierarchic fashion. Usually, you have the ApplicationContext as parent context, and then a hierarchy of child contexts, for example Workbench > Window > Perspective > Part.
Given my experience in E4 development, in the case of a multi-module application, I would store my model objects in the ApplicationContext, using different keys.
Let me explain: the IEclipseContext is an hashtable where you can store multiple objects using different keys. So, each module (fragment, or bundle) in your system, can persist its model data into the IApplicationContext using a different key.
As an example, if you have threee modules, in your system where one is the platform and two are simply "module1" and "module2", you can store the data of each module, in the application context, as follows:
**KEY** **VALUE**
org.myapp.platform Java Object representing data model of the platform
org.myapp.module1 Java Object representing data model of the module1
org.myapp.module2 Java Object representing data model of the module2
By the way, if you are concerned about the data exposed in the context, you could delete a context object, by setting a null value in correspondence of a specific key.
Hope this helps.
You can dig deeper looking at the following references: