Recently, I received some negative feedback about the design of a class that I had originally thought to be innovative. After reflection, however, I agree it is clearly more flawed than innovative.
This post invited the community to join in the review and provide its thoughtful feedback on the design flaws and identify a fix.
Background and Details:
The class in question is a domain object that gets dual-use in the business layer and in the UI. (Sometimes it is necessary.)
The class includes a Display() Property that returns UI-friendly string. The rationale for including this Property is a convenience for class consumers. In my specific use, it was easy to bind a collection of this object to a UI grid control on the Display() Property.
Display is implemented through a delegate so consumers can swap in another delegate.
Implementation:
//Within the "Measure" class is a delegate for the Display()
public delegate string DisplayDelegate(IMeasure Measure);
private DisplayDelegate _displayDelegate;
...
//The class uses a Write-Only Property sets the Display() delegate.
//Within the constructor (not shown here), I set a default Display delegate by
//concatenating a few properties together in a UI-friendly display string.
public DisplayDelegate Display {
set { _displayDelegate = value; }
}
//Class consumers call "MeasureDisplay" for a
public string MeasureDisplay {
get { return _displayDelegate.Invoke(this); }
}
The responses below address the question of what is wrong with the design and what could fix it.