I have a control and it provides a selection mechanism. However, the items that it selects are not appropriate for general consumption and instead, must be projected into a different type parameterised by date.
In addition to this, there are some controls that want to encapsulate the same information and, more importantly, want to retain a particular selection even if the original control's selection has changed. Therefore, just chaining methods doesn't work because the original control's method will always return the projection of its current selection.
To work around this, I created a property that returns a closure, which performs the projection based on a specific selection. This way, I can encapsulate the projection and protect the real type being projected, but also encapsulate a specific collection. Here is a simplified version of what I have.
public class MySelectionControl
{
public Func<DateTime, IEnumerable<MyProjectedType>> CreateSelectionForDate
{
get
{
// Take a copy of the selection before creating the lambda so
// that the source items won't change if the selection does.
IEnumerable<MyRealType> copyOfSelection = this.realSelection.ToList();
return weekEnding =>
copyOfSelection.Select(x => new MyProjectedType(x, weekEnding));
}
}
}
This can then be called like a method as in:
MySelectionControl control = new MySelectionControl();
// Gives the current selection for a given date.
control.CreateSelectionForDate(DateTime.Today);
// Takes a copy of the selection for later use, regardless of
// whether the original selection changes.
var selectedItemsProjection = control.CreateSelectionForDate;
So, is this terrible design or clever use of delegates?