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 example开发者_开发知识库s 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.
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#):
class Model : INotifyPropertyChanged
{
public string Name { get; set; } // this raises PropertyChanged
}
class ViewModel
{
private readonly Model _model;
public Model Model { get { return _model; } }
}
View XAML:
<Textbox Text="{Binding Model.Name}" />
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 an ObservableCollection<Person>
called People
. 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.
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.
精彩评论