tags:

views:

41

answers:

2

I am trying to use MVVM in my WPF application which displays data from a class in the business logic layer called 'Employee' with a property 'Salary'. I have a simple XAML window with a text box with binding to the Salary property in a ViewModel class 'EmployeeViewModel'. The ViewModel implements INotifyPropertyChanged so that when the Salary property of the EmployeeViewModel changes, the value in the XAML text box also changes. All that works fine.

Now, the Salary property of EmployeeViewModel gets its value from the Salary property of the Employee class. However, if the Salary value of the Employee class is changed by some other procedure going on in the business logic layer, it does not automatically update in the EmployeeViewModel. What is the best way to get it to do this? I was thinking some kind of INotifyPropertyChanged implementation in the Employee class which would let the EmployeeViewModel know to update itself. Or should I use a Dependency Property? Or should I have procedure which 'refreshes' the ViewModel once all of the changes in the business logic layer have been completed. Any help appreciated!

A: 

I strongly recommend you to implement INotifyPropertyChanged on your business object(Employee) and register to changes on your view model.

If you really wanna stay as much poco as you can, you can just declare the interface and add a public method that called RaisePropertyChanged(string propertyName) and just call it when your bussiness logic finish.

Chen Kinnrot
Ok, so INotifyPropertyChanged bit seems the way forward. So if I put an OnPropertyChanged on Salary in Employee, then Salary in EmployeeViewModel will automatically update itself each time it changes in Employee? Or do I need some extra code to get EmployeeViewModel to 'listen'?
Caustix
A: 

My thoughts

if you implement INotifyPropertyChanged on your Business Entity, you will create a coupling between your Business Entity and your View Model.

Your Model should not be dependent upon specific feature like INotifypropertychanged.

2- If you are using Prism Framework ,then use EventAgreegator which is a pure decouple way to communicate between modules.

3- your 3rd option point sounds good to me when you say "I have procedure which 'refreshes' the ViewModel once all of the changes in the business logic layer have been completed"

saurabh
Thanks for the help. After some thought I've decided to go with the 3rd option. All of my ViewModels now belong to a collection called AllViewModels which lops through each and calls a Refresh method in each ViewModel. The Refresh method loops thorugh each property in the ViewModel and updates with the corresponding property from the BusinessModel, which in turn triggers OnPropertyChanged.
Caustix