How to enforce an EReference to be initialized on creation of container [message #1461207] |
Tue, 04 November 2014 05:13  |
Eclipse User |
|
|
|
Hi,
I have a question regarding initialization of an EReference which is modeled as a containment to is parent object. Is it possible to have this EReference be initialized upon creation of the container? So that I can omit null checks when accessing the object?
For example when I have a Document with MetaData. I would like to achieve that document.getMetaData() is never null.
Thx.
Thorsten
|
|
|
|
Re: How to enforce an EReference to be initialized on creation of container [message #1461267 is a reply to message #1461207] |
Tue, 04 November 2014 06:35   |
Eclipse User |
|
|
|
Thorsten,
Comments below.
On 04/11/2014 11:13 AM, Thorsten Schlathölter wrote:
> Hi,
> I have a question regarding initialization of an EReference which is
> modeled as a containment to is parent object. Is it possible to have
> this EReference be initialized upon creation of the container?
That depends a bit of when/where you want to do that. Certainly you
need to be very cautious about what you do directly in the model code so
as not to break things like deserialization and copying...
So it's not generally a good idea to initialize an object in the
generated constructor, whoever tempting that may be.
If the purpose is to make the user's life easier, it's always reasonable
to create more fully initialized instances in the item providers, e.g.,
org.eclipse.emf.ecore.provider.EGenericTypeItemProvider.collectNewChildDescriptors(Collection<Object>,
Object)
> So that I can omit null checks when accessing the object?
If the issue is more one how you write the logic programmatically, for a
case like that, you might want to provide convenience operations for
that purpose...
>
> For example when I have a Document with MetaData. I would like to
> achieve that document.getMetaData() is never null.
You might have a method document.getOrCreateMetaData() (not the nicest
name, but you can be creative) that ensures null is never returned...
>
> Thx.
> Thorsten
|
|
|
|
Re: How to enforce an EReference to be initialized on creation of container [message #1461273 is a reply to message #1461268] |
Tue, 04 November 2014 06:42  |
Eclipse User |
|
|
|
On 04/11/2014 12:39, Felix Dorner wrote:
> On 04/11/2014 11:13, Thorsten Schlathölter wrote:
>> Hi,
>> I have a question regarding initialization of an EReference which is
>> modeled as a containment to is parent object. Is it possible to have
>> this EReference be initialized upon creation of the container? So that I
>> can omit null checks when accessing the object?
>>
>> For example when I have a Document with MetaData. I would like to
>> achieve that document.getMetaData() is never null.
>
> Maybe you're served fine by using a customized DocumentItemProvider.
Uhm , that would be the ItemProvider for elements that can contain
Documents, not the DocumentItemProvider itself. Sorry for confusion.
|
|
|
Powered by
FUDForum. Page generated in 0.03941 seconds