What is the difference in ECore between containment and reference?
Asked Answered
C

2

24

When creating references between ECore entities there is the possibility to mark a reference as "containment". Can somebody explain me in easy words what's the difference between a plain reference and a containment? The definitions and explainations I found so far didn't do the trick for me.

Considered answered 25/9, 2010 at 11:2 Comment(0)
S
47

Reference: A reference is a plain "A knows B" relation. Separate references do not influence each other. A can know B and B can know C. Hence, if you have A, you can go to C over B. If you remove the reference, A, B and C will still exist, they just don't know each other any more.

Containment: A containment is the "A has B" kind of relation. Usually used for lists, e.g. "A has multiple B". ECore/EMF can then perform atomic commands on such collections, such as move all objects from one containment to another. It can also enforce constraints, such as a minimum amount of contained objects or a maximum amount of contained objects, or ensuring that the contained object is not contained in any other containment.

Example:

Assume you have an object called ShoppingCart with a reference called Customer and a containment called OrderedProducts. The OrderedProducts has a reference to a Product.

What does this model tell you?

  • You can assign a Customer to the ShoppingCart. If you remove the Customer from the ShoppingCart, the Customer object itself will still exist (e.g. in the database)
  • The OrderedProduct objects need a ShoppingCart to exist. If you remove one from the ShoppingCart, it will cease to exist.
  • Each OrderedProduct has a reference to an existing Product in the database. If you remove one of the OrderedProducts from the ShoppingCart, the Product in the database will still be there - just the order of that product for that specific customer is gone
Supertax answered 25/9, 2010 at 11:35 Comment(2)
Thanks very much. I think a real example like your ShoppingCart was all it needed to flip the switch in my brain.Considered
Analogous to 'association' and 'composition' in UML class diagrams.Rigadoon
M
5

Reference : can be like association in UML

Containment : is the composition relation from UML

Also this can be seen in the EMF diagram, if you set the containment to true, then the line will have a filled side, showing the containment/composition. This is what I have observed.

Macroclimate answered 29/6, 2012 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.