开发者

MVVM WPF Best way to ensure ViewModel is up to date with Business Layer / Back End

开发者 https://www.devze.com 2023-01-13 15:30 出处:网络
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 w

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 开发者_Python百科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!


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"


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消