Anders Norås wrote:
> My Qi4j version is built using three mixins, one for PersonState and  
> two different for the getFullName behavior. Structurally it is very  
> similar to the Hello World-tutorial. What I'd like to do is decide at  
> runtime which of the two behavioral mixins to use when building the  
> composite. Eg:
> 
>          SingletonAssembler assembly = new SingletonAssembler() {
>              public void assemble(ModuleAssembly module) throws  
> AssemblyException {
>                  module.addComposites(PersonComposite.class);
>              }
>          };
>          CompositeBuilderFactory builderFactory =  
> assembly.compositeBuilderFactory();
>          CompositeBuilder<PersonComposite> builder =  
> builderFactory.newCompositeBuilder(PersonComposite.class);
>          builder.stateOfComposite().firstName().set("Nancy");
>          builder.stateOfComposite().lastName().set("Carthwright");
>          PersonComposite nancy_carthwright = builder.newInstance();
>          Assert.assertThat(nancy_carthwright.fullName(),  
> equalTo("Nancy Carthwright"));
> 
>       // I need some way to change the mixin used by the  
> CompositeBuilder<PersonComposite> builder...
> 
>          PersonComposite nancy_carthwright2 = builder.newInstance();
>       // So that the firstName and lastName values are printed in the  
> opposite order.
>          Assert.assertThat(nancy_carthwright2.fullName(),  
> equalTo("Carthwright Nancy"));
>       
> Is there an easy way (not so easy ways are also fine :-) to achieve  
> this?

Changing these kinds of things at runtime is just bad practice and will 
lead to unreadable software, so we don't support this.

However, we do support contexts in a strong way, which can do what you 
want. You can create a custom version of PersonComposite like so:
@Mixins(CustomFullNameMixin.class)
interface CustomPersonComposite
   extends PersonComposite
{}
and if you register this one *instead of PersonComposite* then the 
following code:
CompositeBuilder<PersonComposite> builder = 
cbf.newCompositeBuilder(PersonComposite.class);

is going to use the CustomPersonComposite, as PersonComposite is not 
explicitly registered and CustomPersonComposite comes "closest" by being 
an extension of it. Note that these decisions are done by the assembler 
and not the client code, which is the point here. What you are 
describing is not a client code decision.

A simpler way to do this is possible with Qi4j v0.6 which is out the 
door any minute. Then you can specify your own overriding mixins at 
assembly time like so:
module.addComposites(PersonComposite.class).withMixins(CustomFullNameMixin.class);
which essentially does the same thing, but using assembly rather than 
developer perspective. It's slightly harder to debug though, so use with 
care.

/Rickard

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

Reply via email to