Aye wrote:
Still learning qi4j through real-world use cases I've built before and
running into a road block (but loving the ability to turn a mess of
domain objects into something like an onion!). Here's what I have:

public interface HumanResources {

    @UseDefaults
    Property<List<Position>> positions();

    Position createPosition(String name);
    void deletePosition(Position position);
}

public interface Position {
    Property<String> title();

    @Optional
    Property<String> description();
}

public interface PositionValue extends Position, ValueComposite {
}

public abstract class HumanResourcesMixin implements HumanResources {

    @Structure
    private ValueBuilderFactory vbf;

    public Position createPosition(String name) {

        final ValueBuilder<Position> builder =
vbf.newValueBuilder(Position.class);
        final Position prototype = builder.prototype();
        prototype.title().set(name);

        final Position position = builder.newInstance();
        positions().get().add(position);
        return position;
    }

    public void deletePosition(Position position) {

    }
}


Ok, so HumanResourcesMixin I think needs to implement the positions()
method but I'm stumped about how to do this. I have to tell
HumanResourcesMixin that it needs to use PositionValue as the
implementation for Position. Can you gives point me in the right
direction?!

Actually, if you keep your mixin abstract you don't have to implement it at all. Property<> methods can be implemented by the PropertyMixin, which is added by default in the Composite interface.

Do you intend HumanResources to be an interface used in an Entity? If so, the pattern I am using right now is more like this:
@Mixin(HumanResourcesMixin.class)
public interface HumanResources {

   interface HumanResourceState
   {
     @UseDefaults
     Property<List<Position>> positions();
   }

     Position createPosition(String name);
     void deletePosition(Position position);

 public class HumanResourcesMixin implements HumanResources {

     @Structure
     private ValueBuilderFactory vbf;

     public Position createPosition(String name) {

         final ValueBuilder<Position> builder =
 vbf.newValueBuilder(Position.class);
         final Position prototype = builder.prototype();
         prototype.title().set(name);

         final Position position = builder.newInstance();
         positions().get().add(position);
         return position;
     }

     public void deletePosition(Position position) {

     }
 }
}

In other words, specify the mixin directly in the interface. Only on VERY rare occasions should you not use this default. This will make the use of your role interface HumanResources more predictable, which is good.

Entities using this can then be declared with:
interface PersonEntity
  extends HumanResources,EntityComposite
{}

And that's it.

/Rickard


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

Reply via email to