eclipse e4 basics - data model [message #849088] |
Wed, 18 April 2012 21:57 |
Christopher Riley Messages: 9 Registered: March 2012 |
Junior Member |
|
|
Hi,
I'm trying to best figure out how to integrate my application data model in to an e4 application. I believe that Dependency Injection is what I want(as notifying these classes of updates to the data model would be a nice thing), but I can't seem to get things to work correctly.
Can someone provide with a straightforward way of instantiating a data model class that can be shared between different dialogs/classes?
So far i've tried doing something along these lines, but I think I may be missing some core concept in developing e4 applications. Just a note on the below code, I have the rest of the application framework working to bring up the dialog windows and call code within them, so these functions are getting called, but the application is not doing what I need it to.
public class ApplicationModel {
private String whattoprint = "hello";
@Inject
public MapperApplication() {
}
public void setPrintStatement(String s) {
whattoprint = s;
}
public void print() {
System.out.println(whattoprint);
}
}
public class Class1 extends TitleAreaDialog {
@Inject @Named("appModel") ApplicationModel ma;
private void setDataModelStatement() {
ma.setPrintStatement("set in class A");
}
}
public class Class2 extends TitleAreaDialog {
@Inject @Named("appModel") ApplicationModel ma;
private void printDataModelStatement() {
ma.print();
}
}
Output:
hello
The output from bringing up dialog1 then dialog2 is "hello", so it seems that a new data model is being instantiated, but what I want is a persistent one that can be accessed throughout the application.
Any help on this would be much appreciated!
Thanks,
~Chris
|
|
|
|
Re: eclipse e4 basics - data model [message #849488 is a reply to message #849088] |
Thu, 19 April 2012 07:32 |
Thomas Schindl Messages: 6651 Registered: July 2009 |
Senior Member |
|
|
To make it "persistent" you'll have to put it into the IEclipseContext
(e.g. the one from the MApplication-Element) then it is shared across
the whole application instance.
Tom
Am 18.04.12 23:57, schrieb Christopher Riley:
> Hi,
>
> I'm trying to best figure out how to integrate my application data model
> in to an e4 application. I believe that Dependency Injection is what I
> want(as notifying these classes of updates to the data model would be a
> nice thing), but I can't seem to get things to work correctly.
>
> Can someone provide with a straightforward way of instantiating a data
> model class that can be shared between different dialogs/classes?
>
> So far i've tried doing something along these lines, but I think I may
> be missing some core concept in developing e4 applications. Just a note
> on the below code, I have the rest of the application framework working
> to bring up the dialog windows and call code within them, so these
> functions are getting called, but the application is not doing what I
> need it to.
>
>
> public class ApplicationModel {
> private String whattoprint = "hello";
> @Inject
> public MapperApplication() {
>
> }
> public void setPrintStatement(String s) {
> whattoprint = s;
> }
> public void print() {
> System.out.println(whattoprint);
> }
> }
>
> public class Class1 extends TitleAreaDialog {
> @Inject @Named("appModel") ApplicationModel ma;
> private void setDataModelStatement() {
> ma.setPrintStatement("set in class A");
> }
> }
>
> public class Class2 extends TitleAreaDialog {
> @Inject @Named("appModel") ApplicationModel ma;
> private void printDataModelStatement() {
> ma.print();
> }
> }
>
>
> Output:
> hello
>
>
> The output from bringing up dialog1 then dialog2 is "hello", so it seems
> that a new data model is being instantiated, but what I want is a
> persistent one that can be accessed throughout the application.
>
> Any help on this would be much appreciated!
> Thanks,
>
> ~Chris
|
|
|
Re: eclipse e4 basics - data model [message #849741 is a reply to message #849088] |
Thu, 19 April 2012 12:10 |
Eclipse User |
|
|
|
Christopher Riley wrote on Thu, 19 April 2012 00:57
Can someone provide with a straightforward way of instantiating a data model class that can be shared between different dialogs/classes?
To have the value injected into you class you have to put it either in an IEclipseContext or register it as an OSGi Service. IEclipseContext-s follow a hierarchical pattern: each part has its context, all parts' context have a parent context (perspective context), all perspectives' context have a parent context (Window) and this goes up to an instance of MApplication's context.
To have a data model object shared among all parts of the application put this object on the application context. You can do that by injecting MApplication and doing
((MApplication)application).getContext().set("appModel",yourDataObject) .
After that "yourDataObject" is shared among all the application's context childern.
|
|
|
|
|
Re: eclipse e4 basics - data model [message #850682 is a reply to message #849795] |
Fri, 20 April 2012 08:24 |
|
Hi Tom,
a good point, thanks for mentioning this.
My argumentation would be the following:
In a single user environment, using the MApplication context is IMHO not a good pattern, as the Context does not allow you to query for its keys, hence you have risk in overriding something where, which is using the same key.
I know the risk is currently low, but MApplication might get extended and if larger application start stuffing things in it, this risk grows.
I think the issue is that MApplication is used for framework classes as well as for business logic. As the Context intentially does not provide an API to query it for its content I would avoid using it for business logic, without the need.
Currently Eclipse 4 does not support a multi-user env. If RAP finally supports E4 it is possible to register the domain objects afterwards in the MApplication which would override the OSGi service as the DI implementation would find this one first before going to the OSGi layer.
Best regards, Lars
|
|
|
Re: eclipse e4 basics - data model [message #850737 is a reply to message #850682] |
Fri, 20 April 2012 09:22 |
Thomas Schindl Messages: 6651 Registered: July 2009 |
Senior Member |
|
|
Well this is only a problem of the key you are using and is true for
everything in IEclipseContexts.
So if you are using "my.company.model" as a key you are as save or even
better you are using the FQN of the Model-Class you are storing there
because then you don't have to use @Named.
Tom
Am 20.04.12 10:24, schrieb Lars Vogel:
> Hi Tom,
>
> a good point, thanks for mentioning this.
> My argumentation would be the following:
> In a single user environment, using the MApplication context is IMHO not
> a good pattern, as the Context does not allow you to query for its keys,
> hence you have risk in overriding something where, which is using the
> same key.
> I know the risk is currently low, but MApplication might get extended
> and if larger application start stuffing things in it, this risk grows.
>
> I think the issue is that MApplication is used for framework classes as
> well as for business logic. As the Context intentially does not provide
> an API to query it for its content I would avoid using it for business
> logic, without the need.
> Currently Eclipse 4 does not support a multi-user env. If RAP finally
> supports E4 it is possible to register the domain objects afterwards in
> the MApplication which would override the OSGi service as the DI
> implementation would find this one first before going to the OSGi layer.
>
> Best regards, Lars
|
|
|
Re: eclipse e4 basics - data model [message #850802 is a reply to message #850737] |
Fri, 20 April 2012 10:45 |
|
@Tom: That is a good workaround, still I think using this approach the application itself may accidently override values, especially if people start using this as the "normal" way of sharing there application data.
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.07962 seconds