When writing a WPF application using MVVM, I want to use a WCF service, with methods on it to provide the relevant data from the applications database.
As an example, if my system has a list of Tasks, they are stored in a SQL database. I can put a method on the Web Service to retrieve all tasks from the database.
I can call this method from a ViewModel and store the results in a
public List<Task> Tasks { get; set; }
Then I would bind a control on my View to this property.
I already have something in place for my View to update, when the Tasks property changed (I didn’t put it above to keep the example simple).
My question is, when a new task is added to the database by somebody else, how do I update the Tasks property on the ViewModel?
Do I need to poll the database every x minutes to look for new tasks (Via a method on the WCF service)?
Or can I somehow do something that will update the Tasks property when tasks are added to the database?
You would need to perform some kind of polling to make this work. Changes made to data in your ViewModel are updated directly (via your bindings) because all the changes occur in the WPF app’s memory. Changes to the database, though, will only be known to the database.
You might want to add a method to your WCF service that takes a
DateTimewhich will return all tasks added since a given time. Then, call that method from your ViewModel at a frequency that makes sense for your usage scenarios and expected data update rates. At that point, any news tasks added should be reflected in your View.