polygene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Niclas Hedhman <nic...@hedhman.org>
Subject Entity Lifecycle is flawed, and should be redesigned.
Date Tue, 30 Jun 2015 16:03:05 GMT

Look at the code below;

public T newInstance()
    throws LifecycleException
    String identity;
    // Figure out whether to use given or generated identity
    identity = (String) entityState.propertyValueOf( IDENTITY_STATE_NAME );
    EntityState newEntityState = model.model().newEntityState( store,

EntityReference.parseEntityReference( identity ) );

    entityState.copyTo( newEntityState );
    EntityInstance instance = model.model().newInstance( uow,
model.module(), newEntityState );
    Object proxy = instance.proxy();
    // Add entity in UOW
    uow.addEntity( instance );
    // Invalidate builder
    this.identity = null;
    return (T) proxy;

This is what happens when an entity is created. The Lifecycle#create()
method is called within the prototypeInstance.invokeCreate(), and that is
the only thing it does.

The other very important bit is the uow.addEntity(). Before that, the
UnitOfWork doesn't know about the entity we are in the process of creating.

Ok, so far so good.

In the Lifecycle.create() method, I can imagine two (more are welcome)
distinct use-cases;

   a. Not all of the entity has been initialized, and the create()
initializes some defaults.

   b. I need to create some associated entities to be used together.

For use-case a. the above sequence is necessary. For use-case b. the above
sequence is detrimental if THIS entity is for instance back referenced in
the supporting entity that is being created. It doesn't work.

The question is whether the Lifecycle is simply poorly defined, and instead
of two methods create() and remove(), we should perhaps have some other

Two different approaches come to my mind.

  1. A set of lifecycle interfaces, each with a single method, such as
Creating, Created, Removing and Removed, which are called (if present)
before and after the above section.

  2. The before/after sounds a lot like a "Concern", so perhaps(!) the
entity creation should actually happen as a Mixin on the EntityComposite,
and one can wrap a regular Concern on that method for before and after. And
perhaps there are even 2 stages, one for the EntityBuilder.newInstance()
call and one for the UnitOfWork.complete() call. It is a somewhat crazy
idea, but would like to get insights in what people might think.

Niclas Hedhman, Software Developer
http://zest.apache.org - New Energy for Java

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message