eclipse e4 basics - data model [message #849088] |
Wed, 18 April 2012 17:57  |
Eclipse User |
|
|
|
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 03:32   |
Eclipse User |
|
|
|
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 #850682 is a reply to message #849795] |
Fri, 20 April 2012 04:24   |
Eclipse User |
|
|
|
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 05:22   |
Eclipse User |
|
|
|
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 #853574 is a reply to message #853440] |
Mon, 23 April 2012 02:15  |
Eclipse User |
|
|
|
I'd suggest using Declarative Services (DS) for registering and you
don't need to access the stuff using the service registry just put
@Inject
private IMapperApplication application
in your code.
I still believe you are abusing the OSGi-Service registry but as I
concluded in my last message it of course works.
Maybe I have time this week to write a blog how I would address the problem.
Tom
Am 23.04.12 04:52, schrieb Christopher Riley:
> Hi Everyone,
>
> I think I may have gotten in slightly over my head here. :o
>
> Using an OSGi service worked out for right now. As we don't anticipate
> ever moving to a multi-user platform, do you think this will be adequate
> moving forward with my project? You can take a peak at the code below
> to see how I did this, I would be grateful if anyone could point out an
> glaring errors in what I did, but it seems to be working as needed.
>
>
>
> For anyone who might come here seeking answers here is how I went about
> inserting and accessing my data model:
>
> implement an Interface to your data model
>
> public interface IMapperApplication {
> }
>
>
> provide implementation with noargs constructor
>
> public class MapperApplication implements IMapperApplication {
> public MapperApplication() {
>
> }
> }
>
>
> register OSGi service in activator
>
> IMapperApplication service = new MapperApplication();
> context.registerService(IMapperApplication.class.getName(), service, null);
>
>
> fetch reference in another class
>
> BundleContext b = InternalPlatform.getDefault().getBundleContext();
> ServiceReference reference =
> b.getServiceReference(IMapperApplication.class.getName());
> applicationModel = (IMapperApplication) b.getService(reference);
>
>
>
>
> Thanks,
>
> ~Christopher Riley
|
|
|
Powered by
FUDForum. Page generated in 0.25684 seconds