Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Constructor with parameters?
Constructor with parameters? [message #1753710] Thu, 09 February 2017 08:19 Go to next message
Nicholas Petalidis is currently offline Nicholas PetalidisFriend
Messages: 3
Registered: May 2015
Junior Member
Hi,

I could not find anything on the subject so I want to ask:

How does one specify in EMF that certain values should always be set when an object is created?

Normally, I would use a parameter-full constructor (e.g. that a Person should always have a name and a surname) but I don't seem to be able to define such a thing in EMF. Am I wrong?

Thanks for any answers
Re: Constructor with parameters? [message #1753713 is a reply to message #1753710] Thu, 09 February 2017 09:13 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 5586
Registered: July 2009
Senior Member
Hi

From a modeling perspective, you can define a non-zero lowerbound and a default value, but only for DataTypes.

These default propagate through to the auto-generated Java code.

However, a very important EMF facility is the ability to load and save as XMI. The support for this requires loading to use the default constructor and then eSet each field. Therefore EMF-compatible Java models must support an at least transient state while many fields are ill-defined.

[I suppose a cleverer genmodel could analyze the defaults or a GenAnnotation and synthesize a parametered constructor. Then a cleverer XMILoad could find those elements in order to use the parametered constructor. But that's not the way it is today.]

Regards

Ed Willink

Re: Constructor with parameters? [message #1753722 is a reply to message #1753713] Thu, 09 February 2017 10:37 Go to previous messageGo to next message
Nicholas Petalidis is currently offline Nicholas PetalidisFriend
Messages: 3
Registered: May 2015
Junior Member
Thanks,
I understand the need for parameter-less constructors. It is common not only in EMF but in other frameworks too.

However, even when defining a lower bound on an attribute, in practice in my code I still may end up with a half-created object. EMF will not actually be able to warn you about that. Is my understanding correct?

In this setting, will it thus make sense to somehow assign my object creation to a Builder and thus force the creation of new objects through the Builder?
Re: Constructor with parameters? [message #1753723 is a reply to message #1753722] Thu, 09 February 2017 10:50 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 5586
Registered: July 2009
Senior Member
Hi

EMF has extensive validation of implicit constraints such as multiplicities defined in the metamodel. In some cases there is even live validation so that reports appear fairly rapidly. If you find something that EMF should validate and doesn't raise a Bugzilla.

You can add your own constraints that will be treated in a similar fashion. This may be done directly in Java, or in Xbase using Xcore or in OCL using OCLinEcore.

You may use the EMF Diagnostician to perform validation under your own programmatic control, or Validate menu options interactively.

Regards

Ed Willink
Previous Topic:[CDO] -Performance - 2n queries for fetching n objects.
Next Topic:Merging metamodels
Goto Forum:
  


Current Time: Wed Jan 24 03:42:19 GMT 2018

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

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