polygene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Merlin <p...@nosphere.org>
Subject Re: Entity Lifecycle is flawed, and should be redesigned.
Date Sat, 18 Jul 2015 13:51:02 GMT
Niclas,

Answering a month old mail from you .. sorry it took me so much time.
See below.

Niclas Hedhman a écrit :
> Hi,
>
> Look at the code below;
>
> public T newInstance()
>     throws LifecycleException
> {
>     checkValid();
>     String identity;
>     // Figure out whether to use given or generated identity
>     identity = (String) entityState.propertyValueOf( IDENTITY_STATE_NAME );
>     EntityState newEntityState = model.model().newEntityState( store,
> uow.module(),
>
> EntityReference.parseEntityReference( identity ) );
>
>     prototypeInstance.invokeCreate();
>     prototypeInstance.checkConstraints();
>     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
> system.

Another use case where actual sequence may be flawed:

    c. I need to associate the newly created entity into another already
existing entity.

As the newly created entity is not in the UoW yet it would fail.
Quite close to b. but a bit different.


> 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.

I'd tend to lend towards 1. for its simplicity.
It looks easier to implement, easier to use and easier to document.

Cheers

/Paul


Mime
View raw message