One ViewModel for UserControl and Window or separate ViewModels

前端 未结 2 1248
我在风中等你
我在风中等你 2020-12-01 17:36

I have MainWindow and AddEdit UserControl. Inside MainWindow I render this AddEdit like

2条回答
  •  渐次进展
    2020-12-01 18:11

    As UserControl is maintained separately and not a part of Window content. I would suggest to have separate ViewModel.

    Benefits of having separate ViewModel:

    1. Reusability - In future if you want to do some changes in data related to UserControl (may be some logic change), all you have to go to your ViewModel and update it and it will get reflected across all windows. You don't have to worry to go to each Window's view model and update code.

    2. Testability - In case you want to test logic related to your control (data part me talking here not view part), you can write it in isolation. No need to worry about testing of Window view model code.

    3. Loosely Coupled - More than one people can work in isolation. Say one developer have to update some code related to Main window and other have to update some code related to UserControl. With one ViewModel in place, there will be some overlap and they can't work in isolation since there are dependent on other person to do its work before he/she can plug in his/her code in ViewModel.

    Also check out here for communication between different ViewModels as you might need that to communicate between Window view model and User Control View model to pass selected data in left window.

提交回复
热议问题