I'm revisiting my class tracking (dirty logic), which I wrote last year. Currently I have an uber base class that deals with all the state tracking, but each property whos values I need to track needs to stick to the standard get { return _x; } set { _isDirty = true; _x = value; }
way of working.
After playing with Entity Framework and reading up on the Proxy Pattern, I was hoping there was a nicer way to implement my IsDIrty Logic whilst being able to make use of auto implemented properties?
To be completely honest, I haven't a clue of what I'm talking about. Is there a way I can do something like the following:
public class Customer : ITrackable
{
[TrackState(true)] // My own attribute
public virtual string Name { get;set;}
[TrackState(true)]
public virtual int Age { get;set;}
// From ITrackable
public bool IsDirty { get; protected set; }
}
And then implement a dynamic proxy which will use reflection (or another magical solution) to call another method first before setting the values on the properties with the TrackState
attribute.
Obviously I could easily do this by creating a phyiscal proxy class and use IoC:
public class CustomerProxy : Customer
{
Customer _customer;
public override string Name
{
get { return _customer.Name; }
set { IsDirty = true; return _customer.Name; }
}
// Other properties
}
But I don't fancy having to do this for every object, otherwise there's no benefit from my existing solution. Hope someone can satisfy my curiosity, or at least tell me how EF achieves it.