As Sll stated, an dirty interface is definitely a good way to go. Taking it further, we want collections to be dirty, but we don't want to necessarily set ALL child objects as dirty. What we can do, however is combine the results of their dirty state, with our own dirty state. Because we're using interfaces, we're leaving it up to the objects to determine whether they are dirty or not.
My solution won't tell you what is dirty, just that the state of any object at any time is dirty or not.
public interface IDirty
{
bool IsDirty { get; }
} // eo interface IDirty
public class SomeObject : IDirty
{
private string name_;
private bool dirty_;
public string Name
{
get { return name_; }
set { name_ = value; dirty_ = true; }
}
public bool IsDirty { get { return dirty_; } }
} // eo class SomeObject
public class SomeObjectWithChildren : IDirty
{
private int averageGrades_;
private bool dirty_;
private List<IDirty> children_ = new List<IDirty>();
public bool IsDirty
{
get
{
bool ret = dirty_;
foreach (IDirty child in children_)
dirty_ |= child.IsDirty;
return ret;
}
}
} // eo class SomeObjectWithChildren
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…