Simon
Simon

Reputation: 9021

How can I determine if a property in an object has changed

I have a number of objects in my application which have a large amount of properties. These objects represent database rows (a lightweight ORM). I'd like to know if there is any way I can determine if a property in the object has changed. For instance, I realise I 'could' do it like this...

// Property for showing us whether a value in this object has changed
private bool _HasChanged = false;
public bool HasChanged
{
    get
    {
        return _HasChanged;
    }
    private set
    {
        _HasChanged = value;
    }
}


// My list of properties
private string _FirstName;
public string FirstName
{
    get
    {
        return _FirstName;
    }
    set
    {
        _HasChanged = true;
        _FirstName = value;
    }
}

private string _LastName;
public string LastName 
{
    get
    {
        return _LastName;
    }
    set
    {
        _HasChanged = true;
        _LastName = value;
    }
}

private int _Age;
public int Age 
{
    get
    {
        return _Age;
    }
    set
    {
        _HasChanged = true;
        _Age = value;
    }
}

But if I use the above example, it means a lot of code for each object (some objects have up to 40 properties, and I have about 30 different objects). What I'd like to do is something more like this...

// Property for showing us whether a value in this object has changed
private bool _HasChanged = false;
public bool HasChanged
{
    get
    {
        return _HasChanged;
    }
    private set
    {
        _HasChanged = value;
    }
}


// My list of properties
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }

If I could do the above and somehow globally set my HasChanged whenever any property in the object changes, it would really speed up my development process.

Any ideas or pointers on how I could do this better would be greatly appreciated. This is in C# using NET 4.0 (though I could use 4.5 if it would help!)

Many thanks,

Upvotes: 3

Views: 12204

Answers (2)

Renaud Bancel
Renaud Bancel

Reputation: 893

The commonly used way to implement passive objects indicating property changes is to implements the INotifyPropertyChange interface.

You are perfectly right that implementing any property change system is rebarbative.

But ORMs can commonly generate objects with INotifyPropertyChange pre-implemented.

Else, if you accept to pay for it, you can buy the commercial version of PostSharp. With it, you have just to tag your classes with the [NotifyPropertyChanged] attribute and postsharp will implement the INotifyPropertyChange for you. See www.postsharp.net. This is the first sample on home page.

You can also use the open source https://github.com/Fody program to do this.

The Roslyn CTP (the futur Microsoft C#/VB.NET compiler) includes a mechanism for this. See : Can I use Roslyn for compile time code rewriting? response.

Upvotes: 2

Rob
Rob

Reputation: 11788

You fortunately don't need to reinvent the wheel as there is something called INotifyPropertyChanged that does exactly what you need. It is easy to setup and use and it saves a lot of time. Take a look at this tutorial on how to implement it:

http://csharp.2000things.com/tag/inotifypropertychanged/

Upvotes: 4

Related Questions