Closed. This question needs to be more
focused。它当前不接受答案。
想改善这个问题吗?更新问题,使其仅关注
editing this post的一个问题。
2年前关闭。
Improve this question
背景:
我正在处理WPF解决方案,该解决方案没有按照MVVM模式进行良好的分离。我需要改善架构,可读性等。
我当前的问题是我的MainVM具有大量依赖关系。 MainVM对应于MainWindow View 。 MainVM在启动时创建,并将应用程序的所有其他ViewModel保留为属性。该MainVM传递给Commands的所有调用(实现ICommand的调用),如果某些功能需要其自己的ViewModel,它将引用MainVM并读取包含所需ViewModel的相关属性。
另一个问题是ViewModels没有与Models分离。
我的想法是为ViewModels创建一个“存储”(理想情况下是Models,但此时很难实现分离),并从该存储而不是从MainVM访问所需的ViewModel。
我的应用程序不存储任何内容,因此此ViewModels存储的生存期将与应用程序的生存期相同(静态)。
现在我的问题(改写):
1-从一个“主” ViewModel访问ViewModel是常见的做法吗?
2-在WPF应用程序中对ViewModels和Models使用单独的存储可能有哪些缺点?
3-命令(实现ICommand的人)是否应该依赖于ViewModels?
改写的问题:
1-将 View 模型存储在WPF应用程序中的什么位置?
2-是否可以使用存储库模式在WPF中存储模型?
3-无修改
1-从一个“主” ViewModel访问ViewModel是一种常见的做法吗?
Ans: No. This is a very bad design (if I call it a design). ViewModel
should be designed in a way that it is agnostic of its parent and
children, so that even if its hierarchy changes in future it would not
effect any other module. It should only handle the properties,
commands, events of the view it is backing. Any communication to
children or parent viewmodels should happen via Events. It should
definitly not refer the properties of parent or child VMs via direct
reference. In this way you will have maintainable, testable app and
wont run into the situation you are in right now.
2-在WPF应用程序中对ViewModels和Models使用单独的存储可能有哪些缺点?
Ans: I assume by storage you mean the Model object that ViewModel will expose to View via its properties and commands. This Model should ideally be a business object of your application. Note that single ViewModel can have multiple models depending on the view design and features. But you should never ever access VM from the Model, which I think you are implying in your question. If you are not un-necessarily breaking your view into micro
views to have like zillions viewmodels and models you should be fine.
3-命令(实现ICommand的命令)是否应该依赖于ViewModels?
Ans: Commands also should be written in a manner that they can be
re-used for similar type of operations across views.