Sometimes (pretty often) there is a need to have a class with properties which can have value and a coupled event for when this value changes. Monitorable allows to skip a routine of creating a readonly property with an event. Repeating code like this:
class MyClass
{
private int _property = 0;
public int Property
{
get
{
return _property;
}
private set
{
if (_property == value)
return;
_property = value;
PropertyUpdated(this, EventArgs.Empty);
}
}
}
can be rewritten like this:
public class MyBetterClass
{
// Exposes getter-setter property _property
private readonly MonitorableSource<int> _property = new MonitorableSource<int>();
// Exposes getter-only property Property.Value and event Property.Updated
public Monitorable<int> Property => _property.Monitorable;
}
The end result is not absolutely equal because you need to use .Value
to get property value, but it allows to save a lot of typing and repeating code.