views:

47

answers:

2

In this post i found INotifyPropertyChanged and i check it's example but I notice that i can do the same thing without implement INotifyPropertyChanged and more i can define my event and do the same...

For example in

public string CustomerName
{
    get
    {
        return this.customerNameValue;
    }    
    set
    {
        if (value != this.customerNameValue)
        {
            this.customerNameValue = value;
            NotifyPropertyChanged("CustomerName");
        }
    }
}

I can put any string and it can be passed without any validation and i already do something like that as below

public delegate void ChangeHandler(string item);
public class DemoCustomer2 
{
    // These fields hold the values for the public properties.
    private Guid idValue = Guid.NewGuid();
    private string customerNameValue = String.Empty;
    private string phoneNumberValue = String.Empty;

    public event ChangeHandler OnChange;
    void CallOnChange(string item)
    {
        if (OnChange != null)
            OnChange(item);
    }

    // The constructor is private to enforce the factory pattern.
    private DemoCustomer2()
    {
        customerNameValue = "Customer";
        phoneNumberValue = "(555)555-5555";
    }

    // This is the public factory method.
    public static DemoCustomer2 CreateNewCustomer()
    {
        return new DemoCustomer2();
    }

    // This property represents an ID, suitable
    // for use as a primary key in a database.
    public Guid ID
    {
        get
        {
            return this.idValue;
        }
    }

    public string CustomerName
    {
        get
        {
            return this.customerNameValue;
        }
        set
        {
            if (value != this.customerNameValue)
            {
                this.customerNameValue = value;
                CallOnChange("CustomerName");
            }
        }
    }

    public string PhoneNumber
    {
        get
        {
            return this.phoneNumberValue;
        }
        set
        {
            if (value != this.phoneNumberValue)
            {
                this.phoneNumberValue = value;
                CallOnChange("PhoneNumber");
            }
        }
    }
}

I didn't find any usefully of using it but could anybody guide me if is there any really useful use for these?

+1  A: 

It's not the implementation that is significant about INotifyPropertyChanged, it's the fact that there is an "official" way of doing things in the framework now. So basically what it gives you is a promise that if you implement the interface, your implementing classes will work with all built-in and third-party components that take advantage of it. When you role your own solution, there is no way to know whether or not your code is going to get along well with others.

ckramer
Right! So `INotifyPropertyChanged` is just a public contract as well as great amount of other FCL interfaces.
abatishchev
Yep. MEF is another example of the same thing. I've implemented plug-in functionality before, but MEF is "the official" way to do it now.
ckramer
+2  A: 

The biggest benefit of implementing INotifyPropertyChanged is for standard integration with data binding. In particular, WPF, the MS UI technology, relies heavily on data binding and has good support for classes implementing INotifyPropertyChanged (and INotifyCollectionChanged).

Dan Bryant