N30CY83RC70WN
N30CY83RC70WN

Reputation: 11

Tracking changes of entities from the View Model

I'm trying out the MVVM pattern on WPF and used the WCF Data service to retrieve some records from the database (i.e list of customers). So from the View Model, I'm calling a certain service that retrieves all customers. Is there a way to track changes on the object customer when making changes to it from the view? Something like EntityState.

Please Help!

Upvotes: 0

Views: 3719

Answers (3)

Uri London
Uri London

Reputation: 10797

Say you have a ViewModel class, with a property named 'customers' which is a list of customers. 3 things can be changed:

  1. The customer property in your view model (i.e. you change the entire list with: this.customers = new list
  2. You add or remove items to or from the list.
  3. A change in a property of a specific customer already in the list (e.g. customers[0].address = 'xxx';

WPF Binding mechanism can handle all cases, provided you fire the correct events. Depends on your scenario each case that can happen has to be handled.

Work around #1 simply by saying that your collection of customer cannot be changed. That is, you allocate the list of customers during the construction of your ViewModel, and you don't replace this property.

For #2, best thing is to reuse a .NET object named ObservableCollection. This collection implements INotifyPropertyChanged, and fires the correct notification with the correct argument upon calls to Add, Remove, Clear, etc.

For #3, you need to implement INotifyPropertyChanged for your Customer object. For example:

public class Customer : INotifyPropertyChanged
{
    public PropertyChangedEventHandler PropertyChanged;

    private string _name;
    public string name {
        get { return _name; }
        set { _name = value; ... /* add code to fire exception */ }
    }

    ... // more properties
}

Now, in your view model, have an observable collection

public class ViewModel
{
    public ObservableCollection<Customer> customers { get; private set; }

    ViewModel( ) {
        // Allocate it once during construction
        customers = new Observablecollection<Customer>( )
    }
}

In the UI, you just need to bind. Whatever ItemsControl you are using:

<ItemsControl DataSource="{Binding customers}">
    <!-- The template to display the items here -->
</ItemControl>

Now, you just need to keep in sync the customers collection with the one on the server. Any change you do to customer will be reflected in the UI.

Upvotes: 0

Youp Bernoulli
Youp Bernoulli

Reputation: 5645

You can implement your own change tracking behaviour on the entities. I you have multiple entities that need to be change tracked, I advice to use a common baseclass for implementing the core functionalities of the change tracking.

But using Entity's Framework Self Tracking Entities is also an option. There are already numerous posts about using STE's vs poco (Plain Old Clr Objects) entities. See here:

Introduction to ADO.NET Entity Framework

STE's vs Poco entities

STE's vs Poco

Upvotes: 1

ChrisBD
ChrisBD

Reputation: 9209

If you have your ViewModel data objects inherit INotifyPropertyChanged then you can set a boolean property to show that changes have been made.

Upvotes: 0

Related Questions