The following is a simple example of an enum which defines the state of an object and a class which shows the implementation of this enum.
public enum Status
Apart from the advice of 'consider making your type immutable', here's something I wrote up (and got Jon and Marc to teach me something along the way)
public class Example_Class
{ // snip
// all properties are public get and private set
private Dictionary m_PropertySetterMap;
public Example_Class()
{
m_PropertySetterMap = new Dictionary();
InitializeSettableProperties();
}
public Example_Class(long id, string name):this()
{ this.ID = id; this.Name = name; }
private void InitializeSettableProperties()
{
AddToPropertyMap("ID", value => { this.ID = value; });
AddToPropertyMap("Name", value => { this.Name = value; });
}
// jump thru a hoop because it won't let me cast an anonymous method to an Action/Delegate
private void AddToPropertyMap(string sPropertyName, Action setterAction)
{ m_PropertySetterMap.Add(sPropertyName, setterAction); }
public void SetProperty(string propertyName, T value)
{
(m_PropertySetterMap[propertyName] as Action).Invoke(value);
this.Status = StatusEnum.Dirty;
}
}
You get the idea.. possible improvements: Use constants for PropertyNames & check if property has really changed. One drawback here is that
obj.SetProperty("ID", 700); // will blow up int instead of long
obj.SetProperty("ID", 700); // be explicit or use 700L