A little background:
I've designed my interfaces, which are used by clients, to be as standalone as possible. Therefore Qi4j is quite invisible there. If a client doesn't or can't use Qi4j, it can say

VanillaContext context = VanillaContextProvider.createNewContext();
SomeThing thingie = context.getSomeFactory().createSomeNewThing();

Here creating context does all the Qi4j application assembling, etc, behind the scenes.

Now, when SomeThing is an Entity, currently problems arise with UnitOfWork. From what I understand, now client must have UnitOfWork opened whenever it wants to touch thingie? (OK that sounds a little wrong. :) )

BUT, our client didn't want or couldn't use Qi4j at all! So there must be some way to create new UnitOfWork behind the scenes whenever required. Most appropriate situation for that would be whenever entering entity's method and no UnitOfWork is available. I tried UnitOfWorkPropagation, but that seems to be suitable only when handling entities within tagged methods.

I could do

public interface SomeThingWrapper extends SomeThing, TransientComposite
{
   // mostly call-through methods to entity
}

However, that is not very convient. Additionally, it is problematic when SomeThing is given to some other place as parameter, and the code has to figure out whether the parameter is wrapper or real entity, and if wrapper, how to get hands on real entity? Some kind of mapping maybe? It gets too complicated and too much work IMO.

Now, what really would be great, is that when creating VanillaContext, I could use contextual fragments to add generic concern to all entities. These concerns would create UnitOfWork whenever needed prior actually entering the method. IE same as UnitOfWorkPropagation, but it would affect on ALL methods of SomeThing.

That is currently not possible, as UnitOfWorkException is thrown almost immediately after going through constraints, if UnitOfWork is not open. Question is, is there some specific reason behind this? Could it be better policy to check for UnitOfWork only when it is really needed, ie when property, association, or maybe private mixin is accessed within concern? And of course right after concerns, if none of the concerns accessed entity's state in any way.

Assuming that UnitOfWork MUST ALWAYS be opened whenever even thinking about accessing concerns of Entities, one could use constraints. Then one would need to create a constraint, which would check some value to see if vanilla context is being used, and if so, would create new UnitOfWork whenever needed. For doing this, currently I would need to add at least one tagged parameter in every method of every entity then - dirty hack. :) In this situation those method-constraints me and Rickard discussed earlier would become handy - I would still need to tag every method though, which is error-prone and dull.

Of course, if there is some other simple solution to my problem, I very much would like to hear it. :)


_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev

Reply via email to