MVVM Model
-
Hi, I read about MVVM model. Using MVVM model i can reduce the code behind. What does it Mean? As MVVM model is separating the view, Model and ViewModel. What is the actual use of ViewModel. can't I bind the data from model itself rather than ViewModel? Thanks, Umesh Tayade
-
Hi, I read about MVVM model. Using MVVM model i can reduce the code behind. What does it Mean? As MVVM model is separating the view, Model and ViewModel. What is the actual use of ViewModel. can't I bind the data from model itself rather than ViewModel? Thanks, Umesh Tayade
I also just started programming with MVVM pattern and I had a good start looking at Karl Shifflet's MVVM training "In the Box". Try it out, it should provide you with a brief overview to start with: In the Box MVVM Training[^] Kind regards, Nico
-
Hi, I read about MVVM model. Using MVVM model i can reduce the code behind. What does it Mean? As MVVM model is separating the view, Model and ViewModel. What is the actual use of ViewModel. can't I bind the data from model itself rather than ViewModel? Thanks, Umesh Tayade
The two main advantages I see in using the ViewModel is that it allows you to write unit tests and separates the various concerns making for a cleaner implementation. You can't unit test the code behind your view but you can unit test the ViewModel code. So by seperating your presentation logic from the view itself, you create a more reliable solution. Not sure if you are actually reducing the amount of code. It's just moved to the ViewModel You can bind directly to the model but writing presentation logic (adding properties, sort routines etc) in the model creates performance issues and confuses other developers about the purpose of the model. So by using the ViewModel class to implement this logic, you can avoid these problems. The idea behind MVVM is simple, finding good articles which step you through implementing MVVM can be hard to hard to find but MVVM fits nicely with the WPF/Silverlight data binding model.
"You get that on the big jobs."
modified on Saturday, May 14, 2011 7:34 PM
-
Hi, I read about MVVM model. Using MVVM model i can reduce the code behind. What does it Mean? As MVVM model is separating the view, Model and ViewModel. What is the actual use of ViewModel. can't I bind the data from model itself rather than ViewModel? Thanks, Umesh Tayade
Member 4550493 wrote:
I read about MVVM model. Using MVVM model i can reduce the code behind.
What does it Mean?You are going to write code for any event (say click) in the view model rather than the code-behind for the view. This completely decouples your UI from the logic inside your event handler.
Member 4550493 wrote:
What is the actual use of ViewModel.
The view model is going to act as the intermediary between your UI and the data mmodel.
Member 4550493 wrote:
can't I bind the data from model itself rather than ViewModel?
Yes you can. But its cleaner to bind to the view model (though both approaches are equally valid and there are arguments for using each of them).
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
-
Hi, I read about MVVM model. Using MVVM model i can reduce the code behind. What does it Mean? As MVVM model is separating the view, Model and ViewModel. What is the actual use of ViewModel. can't I bind the data from model itself rather than ViewModel? Thanks, Umesh Tayade
Member 4550493 wrote:
I read about MVVM model. Using MVVM model i can reduce the code behind.
What does it Mean?The idea of the code-behind reduction is that you remove any code-behind that is not strictly to do with the presentation of the data - thus you separate the presentation completely, allowing the presentation to be changed independently of the rest of the application.
Member 4550493 wrote:
As MVVM model is separating the view, Model and ViewModel.
What is the actual use of ViewModel.I think of the viewmodel as being what the name suggests - a model of the view. if you're going to have a form that dispays a list, and allows the user to select an item from the list to "do something" with, then the view model will contain a property holding the list's elements, and functionality to "do something" with an element of that list. You can write tests against the ViewModel without worrying about visual controls, and the ViewModel becomes ,as it were, the specification of the View.
Member 4550493 wrote:
can't I bind the data from model itself rather than ViewModel?
Sure you can - but the idea of the viewmodel its to separate your presentation from dependency on your data - and vice versa. In order to present data to the user, you may need to manipulate that data in some way - and if you do this in the model in order to be able to bind to it, then you're tying your Data to the View. Fine in principal, until something changes As an example. Say you have a Time stored in the DB and you ant to display it - you retrieve it into a model that contains a DateTime value. You want to display the time as an analogue clock. So you need angles for the hands. Easy, you say, add properties of HoursAngle, and MinutesAngle to your model and bind to them. It works - but it's just Not Right, is it? What business has the model got in presenting angles? What if you now decide youwant to display the time as a percentage of the passage of a day? add more properties to the Model? A better way is for the model to present the time as a datetime, and the ViewModel to provide bindable properties, where necessary, to provide the Gui representations. (note this isn't necessarily a real world example - there are other ways of presenting an analogue clock than binding to an HoursAngle propery on a Viewmodel or a Model) I wa