This page was moved to https://cwiki.apache.org/confluence/display/CAUSEWAY/ActionParameters
Click in the link above if you are not automatically redirected in 10 seconds.


You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Actions have three responsibilities.  

  • execution + supporting methods
  • to specify the target object
  • event publishing

Our programming model could provide several syntaxes to put these responsibilities in different places.  It already supports two (standard actions, and mixins).

To compare these syntaxes, we'll use a concrete example:

public class Customer {
    ...
    public Customer placeOrder(Product p, int quantity) { ... }
    public boolean hidePlaceOrder() { return this.isBlackListed(); }
    public String disablePlaceOrder() { return clockService.outsideShoppingHours(); }
    public String disable1PlaceOrder(Product p) { return p.isOutOfStock() ? "Out of stock": null; }                // could also hide parameters similarly
    public Collection<Product> choices0PlaceOrder() { ... }                                                        // or autoComplete0PlaceOrder(String search) { ... }
    public Product default0PlaceOrder() { return orderService.findLastProductPurchasedBy(this); }
    public int default1PlaceOrder() { return 1; }
    public String validate1PlaceOrder(int quantity) { return quantity <= 0 ? "Can only order +ve amounts": null; }
    public String validatePlaceOrder(Product p, int quantity) { return catalogService.runningLowOn(p) && quantity > 4 ? "We're running low on that product, no more than 4" : null; }
    ...
    @Inject CatalogService catalogService;
    @Inject ClockService clockService;
    @Inject OrderService orderService;
}


Standard syntax

The standard syntax uses regular methods on the target object.  Naming conventions are used to associate the action with supporting methods (default, choices, hide, disable and validate).

Here's the same example as before, but with implementation stripped out so that it is easier to compare with the alternate syntaxes that follow:

public class Customer {

    public Customer placeOrder(Product p, int quantity) { ... }
    public boolean hidePlaceOrder() { ... }
    public String disablePlaceOrder() { ... }
    public String disable1PlaceOrder(Product p) { ... }
    public Collection<Product> choices0PlaceOrder() { ... }
    public Product default0PlaceOrder() { ... }
    public int default1PlaceOrder() { ... }
    public String validate1PlaceOrder(int quantity) { ... }
    public String validatePlaceOrder(Product p, int quantity) { ... }
}


Mixins syntax

Mixins change the target, by allowing this set of methods to be moved to a different object target:

public class Customer { ... }

@Action                                                        // or @Mixin(method="act") and a bunch of other annotations
public class Customer_placeOrder() {                           // infer action name from the mixin class name 

    private final Customer target;                             // constructor omitted

    public Customer act(Product p, int quantity) { ... }
    public boolean hideAct() { ... }
    public String disableAct() { ... }
    public String disable1Act(Product p) { ... }
    public Collection<Product> choices0Act() { ... }
    public Product default0Act() { ... }
    public int default1Act() { ...}
    public String validate1Act(int quantity) { ... }
    public String validateAct(Product p, int quantity) { ... }
}

Instead of \@Action, the \@Mixin(method="act") could also be used, with additional annotations on the "act" method.  I've chosen the version with the least boilerplate here.


Parameters syntax (proposed)

Per this thread on slack, we could introduce a Parameters object (in Java 14+, this might be a record) to bring together all of the parameters into a single object.  This would make it easier to avoid issues with numbering etc.

public class Customer {

    public class PlaceOrderParameters {
        Product product;
        int quantity;
    }

    public Customer placeOrder(Product p, int quantity) { ... }

    public boolean hidePlaceOrder() { ... }
    public String disablePlaceOrder() { ... }
    public String disable1PlaceOrder(PlaceOrderParameters params) { ... }
    public Collection<Product> choices0PlaceOrder() { ... }              
    public Product default0PlaceOrder() { ... }
    public int default1PlaceOrder() { ... }
    public String validate1PlaceOrder(PlaceOrderParameters params) { ... }
    public String validatePlaceOrder(PlaceOrderParameters params) { ... }
}


There are two variants here.  The first just uses the Parameters object for the supporting methods:


public class Customer { ... }

@Action // or @Mixin(method="act") and a bunch of other annotations
public class Customer_placeOrder() { // infer action name from the mixin class name

private final Customer target; // constructor omitted

public Customer act(Product p, int quantity) { ... }
public boolean hideAct() { ... }
public String disableAct() { ... }
public String disable1Act(Product p) { ... }
public Collection<Product> choices0Act() { ... }
public Product default0Act() { ... }
public int default1Act() { ...}
public String validate1Act(int quantity) { ... }
public String validateAct(Product p, int quantity) { ... }
}




This would also be supported within mixins, for no extra work




  • No labels