Eclipse Community Forums - RDF feed
https://www.eclipse.org/forums/
Eclipse Community ForumsConstructor with parameters?
https://www.eclipse.org/forums/index.php/mv/msg/1084273/1753710/#msg_1753710
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]]>Nicholas Petalidis2017-02-09T08:19:13-00:00Re: Constructor with parameters?
https://www.eclipse.org/forums/index.php/mv/msg/1084273/1753713/#msg_1753713
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
]]>Ed Willink2017-02-09T09:13:59-00:00Re: Constructor with parameters?
https://www.eclipse.org/forums/index.php/mv/msg/1084273/1753722/#msg_1753722
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?]]>Nicholas Petalidis2017-02-09T10:37:14-00:00Re: Constructor with parameters?
https://www.eclipse.org/forums/index.php/mv/msg/1084273/1753723/#msg_1753723
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.