MVVM 设计考虑
我目前正在开发一个新的 WPF 应用程序,并开发了大部分业务逻辑层(即我的模型)。
我要实现 ViewModel 类来表示我的应用程序的一项功能。我对模型-视图-视图模型模式很陌生,我有一个问题:在实现我的 ViewModel 类时最好使用哪种方法。
从网上的例子中我发现模型通常是视图模型的成员。使用这种方法,ViewModel 公开模型成员的属性,以便它们可以绑定到视图中的模型。
例如:
Public Class MyViewModel
Implements INotifyPropertyChanged
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Private _myModel As ModelClass
Public Property MyModelPropertyA As Object
Get
Return _myModel.MyModelPropertyA
End Get
Set(ByVal value As Object)
_myModel.MyModelPropertyA = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("MyModelPropertyA")
End Set
Public Property MyModelPropertyB As Object
Get
Return _myModel.MyModelPropertyB
End Get
Set(ByVal value As Object)
_myModel.MyModelPropertyB = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("MyModelPropertyB")
End Set
'.... And so On'
End Class
我不喜欢这种方法的是,我将重写很多属性。
因此,我正在考虑继承 ViewModel 中的模型类而不是使用私有成员的选项。
像这样:
Public Class MyViewModel
Inherits MyModel
Implements INotifyPropertyChanged
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
'Now all of my properties are inherited'
End Class
第二种方法的问题是我不确定如何在应用程序运行时将模型转换为视图模型。
您无法设置 viewModelInstance = ModelInstance。
(但您可以设置 modelInstance = viewModelInstance)
我正在寻找有关如何实现 ViewModel 类的最佳方法的建议。
I am currently developing a new WPF application and have the majority of my business logic layer developed (ie my Models).
I am about implement ViewModel classes to represent one feature of my application. I am quite new to the Model-View-ViewModel pattern and I have a question about which approach would be best to use when implementing my ViewModel classes.
From examples online I have been finding that often the Model is a member of the ViewModel. Using this approach, the ViewModel exposes the properties of the Model-member so that they can be bound to the Model in the View.
For example:
Public Class MyViewModel
Implements INotifyPropertyChanged
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Private _myModel As ModelClass
Public Property MyModelPropertyA As Object
Get
Return _myModel.MyModelPropertyA
End Get
Set(ByVal value As Object)
_myModel.MyModelPropertyA = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("MyModelPropertyA")
End Set
Public Property MyModelPropertyB As Object
Get
Return _myModel.MyModelPropertyB
End Get
Set(ByVal value As Object)
_myModel.MyModelPropertyB = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("MyModelPropertyB")
End Set
'.... And so On'
End Class
What I don't like about this approach is the fact that there are a lot of properties that I will be re-writing.
So, I am considering the option of Inheriting the model class in the ViewModel instead of using a private member.
Like So:
Public Class MyViewModel
Inherits MyModel
Implements INotifyPropertyChanged
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
'Now all of my properties are inherited'
End Class
The problem with the second approach is that I'm not sure how to convert my models into view models while the application is running.
You can't set viewModelInstance = ModelInstance.
(But you can set modelInstance = viewModelInstance)
I'm looking for advice on the best approach on how to implement the ViewModel classes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
甚至不要考虑从模型继承你的 viewModel - 这将是一个没有人会喜欢的黑客。如果您太懒而不愿公开所有属性(顺便说一句,resharper 可以自动执行此操作),那么您可以将模型包含到 viewModel 中,并通过某些只读属性提供对它的访问。但是您仍然应该在模型类中实现
INotifyPropertyChanged
。一些代码(抱歉是 C#):
查看 XAML:
Do not even think about inheriting your viewModel from model - this will be a hack which nobody will like. If you are too lazy too expose all the properties (BTW resharper can do it automatically) then you can include your model into your viewModel and provide an access to it via some readonly property. But you should still have
INotifyPropertyChanged
implemented in model class.Some code (sorry for C#):
View XAML:
ViewModel 通常包装/封装与视图相关的逻辑。恕我直言,不需要使用 ViewModel 来简单地传递模型数据。虽然纯粹的方法是定义一个 ViewModel 并传递此数据;在某些情况下,根本不需要这样做,因为应用程序本质上很简单。如果应用程序具有任何增长潜力,则需要使用 ViewModel。
如果您有一个
Person
模型; ViewModel 通常可能包含一个公开名为People
的ObservableCollection
的属性。 ViewModel 是 View 的协调器;不是模型的传递。然而,由于上述原因,您不应该将模型与 ViewModel 绑定在一起,因为它们在理论和实践中都应该相互解耦。
The ViewModel typically wraps/encapsulates logic relational to the View. Using a ViewModel to simply pass through the Model data is un-needed IMHO. While a purist approach would be to define a ViewModel and pass this data through; under certain circumstances this is simply not needed as the application is simplistic in nature. If the application has any growth potential then using a ViewModel would be necessary.
If you had a
Person
Model; the ViewModel may typically contain a property which exposes anObservableCollection<Person>
calledPeople
. The ViewModel is the orchestrator for the View; not a pass through for the Model.You should however not tie your Model to the ViewModel for reasons mentioned above as they should be decoupled from each other in both theory and practice.
查看取自此处的图表。
这是一个值得参考的优秀图表,以确保您正确遵循该模式。正如您所看到的,层之间的交互有多种方式,但最主要的是分离。始终确保每个层仅了解其父层而不了解其子层,即VM了解模型,但不了解视图,并且模型了解业务层而不了解视图模型或视图。
正如您从箭头中看到的(以及其他人提到的),模型可以“通过视图模型上的单个属性公开”,这意味着视图可以通过此直接链接到模型,或者模型可以“抽象”或在虚拟机上的“模型属性”中重新实现。
Check out this diagram taken from here.
This is an excellent diagram to refer to in order to ensure you are following the pattern correctly. As you can see there are various ways to interact between the layers, but the main thing is that seperation. Always ensure that each layer only knows about it's parent layer and not it's children, i.e. VM knows about the Model, but not the view, and the Model knows about the Business layer and not the view model or view.
As you can see from the arrows (and as others mentioned) the Model can be 'exposed through a single property on the viewmodel', which means the view then has a direct link to the model via this, or the Model can be 'Abstracted or re-implemented in Model properties' on the vm.