Tracking dirtiness with DynamicProxy

When dealing with POCOs, a common scenario is to track changes. Look at this interface:

It is the interface of a simple view class containing the name and weight of a product. The Modified property is used to track whether the object has changed its value, so that we know if we should save the object after it has been edited.

A typical implementation might look like this:

It can be seen above that modified is set to true if a change is detected whenever a setter is called. This is pretty straightforward, yet it seems that an annoying pattern emerges when there is more than a few properties.

How can we then get rid of all the boilerplate code, all the noisy bits in between? Well, there is a way: by using Castle‘s DynamicProxy and a simple technique from aspect oriented programming called method interception. Read on to see an example.
Read more