Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » eclipse e4 basics - data model
eclipse e4 basics - data model [message #849088] Wed, 18 April 2012 21:57 Go to next message
Christopher Riley is currently offline Christopher RileyFriend
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 #849127 is a reply to message #849088] Wed, 18 April 2012 22:49 Go to previous messageGo to next message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1049
Registered: July 2009
Senior Member

I would use an OSGi Service and inject that one.
Re: eclipse e4 basics - data model [message #849488 is a reply to message #849088] Thu, 19 April 2012 07:32 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 5487
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 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot CelaFriend
Messages: 597
Registered: December 2010
Senior Member

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 #849751 is a reply to message #849741] Thu, 19 April 2012 12:23 Go to previous messageGo to next message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1049
Registered: July 2009
Senior Member

I think putting domain objects in the MApplication scope is not a good pattern. I think non-framework functionality should preferable be defined in the OSGi service layer.
Re: eclipse e4 basics - data model [message #849795 is a reply to message #849751] Thu, 19 April 2012 13:03 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 5487
Registered: July 2009
Senior Member
Sorry to totally disagree with you here.

OSGi-Services are singletons and hence will bring a whole lot of
problems once you want to run in a multi-user env - the application
context is the correct position to store application scoped informations.

Tom

Am 19.04.12 14:23, schrieb Lars Vogel:
> I think putting domain objects in the MApplication scope is not a good
> pattern. I think non-framework functionality should preferable be
> defined in the OSGi service layer.
Re: eclipse e4 basics - data model [message #850682 is a reply to message #849795] Fri, 20 April 2012 08:24 Go to previous messageGo to next message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1049
Registered: July 2009
Senior Member

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 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 5487
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 Go to previous messageGo to next message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1049
Registered: July 2009
Senior Member

@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.
Re: eclipse e4 basics - data model [message #850827 is a reply to message #850802] Fri, 20 April 2012 11:11 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 5487
Registered: July 2009
Senior Member
IMHO the term service implies some action so there's a
DataAccessService, ... but IMHO they should be stateless and don't hold
any data.

Data is something flowing through services but they don't store them the
simple reason for this is that if they are stateless you can easily
transform them into remote services, make them dynamic
loadable/replaceable, don't need to refactor your code when it should be
used in a multi-user env, ... .

If someone wants to stump on my namespace he'll do it anyway even with
OSGi-Services - he just has to register a higher ranked service and mine
is gone.

If you push everything in the OSGi-Context (you are proposing nothing
more than that by using OSGi-Services for storage) we are back in the
Singleton 3.x world.

I know this is a matter of taste to some extend though and while your
proposal is not wrong I think the best storage for Application-Shared
informations is the IEclipseContext attached to the MApplication.

Tom

Am 20.04.12 12:45, schrieb Lars Vogel:
> @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.
Re: eclipse e4 basics - data model [message #853440 is a reply to message #850827] Mon, 23 April 2012 02:52 Go to previous messageGo to next message
Christopher Riley is currently offline Christopher RileyFriend
Messages: 9
Registered: March 2012
Junior Member
Hi Everyone,

I think I may have gotten in slightly over my head here. Surprised

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
Re: eclipse e4 basics - data model [message #853574 is a reply to message #853440] Mon, 23 April 2012 06:15 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 5487
Registered: July 2009
Senior Member
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
Previous Topic:XWT loaded event lookup problem
Next Topic:disabling handledmenuitem
Goto Forum:
  


Current Time: Thu Dec 18 02:25:21 GMT 2014

Powered by FUDForum. Page generated in 0.03236 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software