Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to enforce an EReference to be initialized on creation of container
How to enforce an EReference to be initialized on creation of container [message #1461207] Tue, 04 November 2014 10:13 Go to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 309
Registered: February 2012
Location: Düsseldorf
Senior Member
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 #1461259 is a reply to message #1461207] Tue, 04 November 2014 11:29 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 853
Registered: July 2009
Senior Member
You could provide your own service API to create your business objects, for example:

IDocumentService#createDocument()


The impl of that method would call the generated model Factory and "decorate" the object as needed...

Thorsten Schlathölter wrote on Tue, 04 November 2014 11:13
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 11:35 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30559
Registered: July 2009
Senior Member
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 #1461268 is a reply to message #1461207] Tue, 04 November 2014 11:39 Go to previous messageGo to next message
Felix Dorner is currently offline Felix DornerFriend
Messages: 295
Registered: March 2012
Senior Member
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.
Just look in the createNewChildDescriptor. There, the Document object is
instanciated, but nothing prevents you from also instantiating a
Metadata object and establishing the reference Doc->Metadata here. When
the user then clicks the Add->Document, the document already has its
Metadata child.
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 11:42 Go to previous message
Felix Dorner is currently offline Felix DornerFriend
Messages: 295
Registered: March 2012
Senior Member
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.
Previous Topic:EclipseCon NA 2015: Call for Papers
Next Topic:Help me to fix reset list of command execution
Goto Forum:
  


Current Time: Thu Oct 24 01:56:43 GMT 2019

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

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

Back to the top