1

I am trying to implement a very simple and more importantly optional object tracking system similar to how entity framework tracks object changes (How change tracking works in Entity Framework).

I have a base class that all other objects inherit from. This class has 1 boolean field called modified. The explicit solution to this is to update every setter on all properties of other classes to set modified = true when the setter is triggered. This solution has been presented in detail here Create an event to watch for a change of variable.

I want a more implicit solution to this. I have a lot of objects and a LOT of properties. Updating the setter is incredibly messy. I don't want to directly mimmic how entity framework does things because it is too expensive for my current requirements. I want a list of objects that i can loop to check of modified == true. This allows me to optionally track object and quickly check if they have changed without having to trigger an update for each individual object. Is there a way that i can set some sort of listener on all of the property getter and setters implicitly?

I know this code doesnt exist, but does .NET have a way to monitor the object to see if it has changed.

[OnChange=ObjectChanged()] //maybe this way
public class MyClass
{
    bool modified {get; set;}
    public MyClass() : OnChange(ObjectChanged) //or this way
    {

    }

    private void ObjectChanged()
    {
        modified = true;
    }
}

As i mentioned, i dont want to update every setter or copy what entity framework does.

Dan Hastings
  • 3,241
  • 7
  • 34
  • 71
  • 1
    Possible duplicate of [How to check if an object has changed?](https://stackoverflow.com/questions/7952616/how-to-check-if-an-object-has-changed) – Adam Aug 09 '17 at 15:13
  • 1
    @Adam please read the question before suggesting it is a duplicate based on the title alone. – Dan Hastings Aug 09 '17 at 15:15
  • 1
    @DanHastings or, perhaps read the *answers* not just the question to determine if its close enough to your problem to point you in the right direction. A duplicate doesnt mean the question is the same - it can also be the answers would be the same – Jamiec Aug 09 '17 at 15:16
  • @Jamiec: [An applicable answer from a different question does not automatically make the question a duplicate.](https://meta.stackexchange.com/q/74080/138661) Please don't forget that the main purpose of SO is not only to help the OP but to build a repository of great, easy-to-find questions and answers. – Heinzi Aug 09 '17 at 15:28
  • @Heinzi not always but its a good indication it might be a duplicate if were talking something as generic as "determine if a property has changed" – Jamiec Aug 09 '17 at 15:48
  • @Jamiec: True, of course. In this case, I think the other question is not a very good dupe target for this question, even if we only consider the answers: Both the accepted answer and the most-upvoted answer of the dupe target do not apply to this question. However, I'll be happy to use my dupe-hammer if there is a more suitable target. – Heinzi Aug 09 '17 at 15:59

1 Answers1

6

There's nothing built-in in the .NET framework for this, but there are third-party tools to do exactly that.

One such option would be Fody/PropertyChanged, which injects OnPropertyChanged calls into all automatic property setters of your classes. You can subscribe to the PropertyChanged event and update your Boolean flag. As an additional bonus, (a) you get the name of the property that changed and (b) other classes (e.g. your UI framework) can subscribe to the event as well.

Heinzi
  • 167,459
  • 57
  • 363
  • 519