This project is read-only.

Contributed actions on collections, failure on "successive" invocations

Aug 29, 2012 at 9:50 AM
Edited Aug 29, 2012 at 9:51 AM

[Originally posted by Dan Haywood as an Issue  -   moved to Discussions]

Experimenting with a design pattern to exploit contributed actions on collections.  
Ideally, would like to provide the facility to perform an action on an internal collection (eg OrderLines for an Order).  However, contributed actions do not support this.
So the idea instead is to "export" the collection into a standalone collection, and have the contributed action always return this same collection (for subsequent invocations).
The following code sketch should show what I mean:

public class Order {    public int Id { get; set; }
    public ICollection<OrderLine> OrderLines { get; set; }
public IQueryable<OrderLine> WorkWithOrderLines {  return Container.Instances<OrderLine>().Where(ol => ol.OrderId = Id); }}
public class OrderLine {    public int OrderId { get; set; } ...    public bool Rush { get; set; }}

public class OrderLineContributedActions {    public IQueryable<OrderLine> Rush(IQueryable<OrderLine> lines) {    return ForEach(lines, ol => ol.Rush = false); }    public IQueryable<OrderLine> UnRush(IQueryable<OrderLine> lines) {    return ForEach(lines, ol => ol.Rush = false); }
public IQueryable<OrderLine> ForEach(IQueryable<OrderLine> lines, Action<OrderLine) {    var list = lines.ToList();        if (list.Count == 0) { return null; }
foreach (var ss in list) { func.Invoke(ss); }
// return all lines for the parent order var firstLine = list.First<ServiceSelection>(); return firstLine.Order.WorkWithOrderLines; }}

Thus, an Order's WorkWithOrderLines() action can be called, which returns a standalone list of its OrderLines.  Since this is an IQueryable<OrderLine>, the OrderLineContributedActions contributes the Rush(...) and DeRush(...) actions to this collection.  When invoked, the action returns the very same IQueryable<OrderLine>; from the UI it looks like we are staying on the "same page"
The problem is that, although the action invokes fine the first time, on the second attempt it throws an exception.