Hey
Interesting questions, and I think private mixins is what you're looking
for to solve your problems. Comments inline below.
Richard Wallace wrote:
> In the first case, the iterations property should be Immutable because
> only the ItemStyle itself should manage the collection. But that only
> limits calling the style.iterations().set() method, but users are free
> to modify the collection by calling style.iterations().get() and using
> the regular collection modifiers. Could it, and should it, also make
> sure that the collection returned by get() is unmodifiable?
What you could do is combine the two alternatives to get the best of
both worlds. First you do an interface with the SetAssociation, which
points to Entities. That interface is then *not* included in the
Composite interface, but is only accessed internally through @This. Like so:
public interface Iteration extends Nameable, EntityComposite {...}
public interface ItemStyle extends Nameable
{
SetAssociation<Iteration> iterations();
}
then the public interface to be included in the Composite:
public interface ItemStyleAggregate
{
ImmutableProperty<Collection<Iteration>> iterations();
Iteration newIteration(@NonEmptyString String name);
}
then the implementation of ItemStyleAggregate:
public class ItemStyleAggregateMixin
implements ItemStyleAggregate
{
@This ItemStyle itemStyle;
@Structure UnitOfWorkFactory uowf;
public ImmutableProperty<Collection<Iteration>> iterations()
{
return new ComputedPropertyInstance<Collection<Iteration>>(...)
{
public Collection<Iteration> get()
{
return Collections.unmodifiableSet(itemStyle.iterations());
}
}
}
public Iteration newIteration(String name)
{
// Check that no iteration with given name already exists
...
UnitOfWork uow = uowf.currentUnitOfWork();
Iteration iteration = uow.newEntity(Iteration.class);
iteration.name().set(name);
itemStyle.iterations.add(iteration);
return iteration;
}
}
This way you can expose an unmodifiable set, and even limit it to be an
Iterable if you want, and still work with it internally as a fullblown
Set, with your domain rules being enforced. There's no need to
"initialize" the property since it is computed on every get.
AFAICT this solves all your requirements. Let me know if something is
unclear.
/Rickard
ps. This is mail-ware, so there might be some typos in the above.
_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev