Understanding Models and ViewModels in MVVM - wpf

I had been developping using WinForms for about a year now and I recently started discovering WPF. I have been more and more interested in this concept and started looking into the WPF's Framework MVVM.
MVVM
We can say that Windows, Pages, UserControls in WPF are Views
But what is the difference between Models and ViewModels?
I've looked at a lot of documentation on MSDN and some videos on YouTube trying to explain this.
If I understand correctly, the Models are basically the structure of your object (for example, Customers) and the ViewModels is what can work WITH the Customers object. Therefore I would bind my Window's DataContext to my ViewModel?

Did you read the MVVM article from MSDN mag? I'm guessing yes, since you're mentioning customers... msdn.microsoft.com/en-us/magazine/dd419663.aspx
If you've been coding OOP in winforms, then you've got classes to represent your data. That's nearly a 1:1 for what a model is. As a super-simplified way of looking at a viewmodel, think of it as the code that previously went into your code-behind for the controls of the page. It tells the view how to draw itself.. buttons to show/enable and the like.
So, in summary,
model::data as viewmodel::form_controls

Related

Is MVVM pattern a symbiote of MVC + PAC patterns?

I've surfed Wikipedia and have found such an article:
http://en.wikipedia.org/wiki/MVC4WPF
A part from the link upper:
MVC4WPF is an open-source, extensible, automated code pattern developed at Information Control Corporation for Windows Presentation Foundation (WPF) development based on the Model-View-Controller (MVC) and Presentation-Abstraction-Control (PAC) patterns...
I know, that WPF/Silverlight do use MVVM pattern: Model-View-ViewModel.
So is MVC4WPF a first version of MVVM?
I don't know the history of WPF/Silverlight development well, but MVVM has always remind me some sort of MVC.
And if it's true, then MVVM = MVC + PAC?
Whenever I see questions like this, I always think about Dr. WPF's design pattern.
It really doesn't matter what you want to call your separate concerns. You will hear a lot of WPF people talk about MVVM, but all it really boils down to is trying to keep your code separate. In MVVM, you have:
Model All your data classes. Knows nothing about anything.
View You gotta show your data. It knows about the Models, but not how to get them. "Knows" about the ViewModel through Bindings. Depending on how you wire them together, it may even create the ViewModel.
ViewModel This is the glue between the View and the Model. It can know about the View, or you can do your best to hide the view from the ViewModel.
In MVC you have:
Model All your data classes. Knows nothing about anything.
View You gotta show your data. Knows about Models, but not how to get them.
Controller This is the glue between the view and the model. It knows about the View and the Model.
Really, the last bit is all that changes (hence Dr. WPF's MV-poo). And in WPF, Bindings in XAML are so nice why would you want to write a bunch of code? Call it whatever you want, it is easier in WPF if the View knows a bit about the poo.
Having never used the PAC pattern, I can't speak to its strengths, but from Wikipedia, it seems to be very similar to MVC. So, I would classify it as a bit of MV-poo.
Now, considering that MVC4WPF hasn't had a release since 2009 (and their website with documentation is down as of 7/6/13), I would suggest you steer clear of it. I can't speak to its strengths or weaknesses, but if you'd like a good MVVM framework, MVVM Light and Caliburn.Micro both have good support and great reviews.
I'd also point out that not all applications need the poo. Mike Hillberg put this nicely.

Ways to attached an ICommand to a control in XAML

I'm very new to XAML. To utilize MVC architecture and the Command Pattern while taking advantage of XAML, I have started binding static ICommands to Buttons. I'm working on a fairly large project with over a hundred buttons. My questions are: are there different approaches for binding commands to buttons to avoid static objects. With regards to C#, WPF, and XAML, are statics commonly used? I'm sure someone has already worked on a project using MVC, Command Pattern, and XAML, what was your approach?
I should have probably edited this sooner, but while working on the project, I've realized how much I didn't know about c#, WPF, and XAML when I asked this question. Apparently, in WPF, instance properties make it convenient binding methods and data members to controls.
As far as MVC / MVVM are concerned, I guess I was hesitant to expose my model to VM before I even know what it is.
I think you mean the MVVM pattern as it applies to WPF. That is Model View ViewModel
Model = used to construct the form model for the data being manipulated
View = Presentation (usually a main form and many user control forms)
ViewModel = code container for presenter (classes that contain the code)
Generally your binding take the place of ICommand methods that implement a RelayCommand from a base. There is a lot to learn before implementing the MVVM model, I would suggest reading Josh Smith's article and downloading his example to get started on learning it:
There are special rules and principles to learn and this example will go through a lot of it.
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
I would also think it would be wise to learn how WPF performs bindings a little bit. There are many special setups you can do with bindings to help with performing operations at different events and other places. I do not even know all the bindings by heart but I know the more you learn them the more time you save in the end in your XAML coding as you can reuse, event trigger, inherit and do many things with bindings to make your applications more powerful than static creation.
http://msdn.microsoft.com/en-us/library/ms752347.aspx

MVVM - Pertaining to WPF command binding standards

I think I have a pretty good understanding of the MVVM design model, however I have a quarm with it in regards to WPF, Command bindings and how we are meant to use them.
To bind commands to the XAML directly we are meant to implement the ICommand interface within the ViewModel. Now, the ICommand interface is part of the PresentationCore.DLL, which, correct me if im wrong is part of WPF not the base .NET framework.
Isnt the whole point of the ViewModel and Model that it should be totally UI independant? For example, if I implement ICommand in my ViewModel and use it as a data context in order to bind commands from the XAML, isnt my ViewModel then dependant on the WPF frame work (in particular the PresentationCore.Dll).
What I mean is, if I was to go and try to use my Models and ViewModels in lets say a Windows Forms environment, I would have to reference the PresentationCore.DLL even though I shouldnt need it because im using Windows Forms not the WPF framework.
This seems a bit odd to me, am I missing something here? Is there another way I should be doing it to keep my Model and ViewModel totally UI and UI Framework independant, but still be able to utilise the Command binding in XAML?
Thanks in advance!
I also had this kind of problem but not in wpf but in POCO classes. What i did was I created two partial classes in two different assemblies. Like you create one partial class which is not presentationcore.dll dependent in your VM project and create its partial class in another assembly(say WPFVM) which implements ICommand stuff. Now for Winforms stuff add only VM project reference to View project and for WPF stuff add references of both VM and WPFVM to the View project. I hope this will help.
The point of MVVM is to have the view just be a view, and nothing more. Putting ICommands into the view model helps this as it pulls the code away from the view. Where you will run into problems is if you have to access something on the view that is not a dependency property, which means you can not bind to it.
In my opinion MVVM is very popular with the WPF, Silverlight because it naturally fits into it. The data binding concept in the XAML allows the Views & ViewModels to be bridged using a single property which is the DataContext. As no longer your logic is tied to controls, you get better testability, design-code separation and maintainability. You may be able to implement the MVVM pattern in other places also, but in WPF and Silverlight, it fits so easily due to its data and command binding support. I have read somewhere that, Don't take patterns religiously. They were made to make your life simpler rather than giving you more problems while following it. For Winforms i think there are better patterns, If you are focusing in reusing the business logic, move them out of your ViewModels to seperate classes something like serviceproviders or serviceagents and share them between your Winforms and WPF apps.
This has changed in .NET 4.5 compare
.NET Framework 4.5
.NET Framework 4

Where should the viewModel be created?

I have seen a few examples where the viewModel (in Silverlight apps) is in the UserControl.Resources XAML section of a View. I read that for using Blend, this is a good place to have it (as it gives the ability to see sample data in Blend).
However, is this the best place to have the viewModel? I read that the "view has to push services to the viewModel". What does this mean and where else could or should the ViewModel be created?
Thanks.
JD.
There are lots of ways that the View and the ViewModel can be connected. The simplest approach is using the Resources like you mention or even easy just setting the DataContext of the View in the Xaml to an instance of the ViewModel.
From there things get more complex and really it depends on the framework you use:
Silverlight.FX - Uses a View base class with a Model property.
MVVM Light - Uses a ViewModelLocator.
Prism - Controllers
Caliburn - Presenters
So the approach you take will depend on what style you like. There are many ways to do this and right now there are a lot of MVVM frameworks showing up.

Confusion regarding MVVM pattern and dynamic loading of XAML in GUI

Well this question relates to MVVM pattern and i could good and fast answers on this forum so I thought to ask and clear the confusions i had about the pattern.
I am quite new to MVVM approach. I appreciate the pattern and understand the principals behind it. Maybe I have not worked that much with the pattern that’s why there are a few confusions.
If there is a scenario in which I want to load few parts of my WPF page dynamically with XAML and still want to be compliant with MVVM approach.
The confusion is:
Where the logic of loading a view dynamically with XAML reside.
Whether I should have a single ViewModel for my WPF page or each seperate part have its own viewmodel with interactions with other viewmodel classes.
What if I had to build control tree displayed on the GUI using C# code in the codebehind itself.
For the controls created using code should I do the commandbindings in the codebehind of the view itself.
Where the logic for loading goes is something not really addressed by the pattern itself. There's an interesting blog post about this by Ward Bell. There's any number of ways to skin this cat, and they're all compatible with MVVM. Not really the answer you're looking for, I know, but it's honest :). Check out Ward's blog post... you'll get a much more in depth discussion of this topic.
As for whether or not to have a single VM for the page, or one for each control, that just depends. Generally, I have one for the page. If there's some portion that reusable elsewhere, I break it out into a user control with it's own VM, which means we have a VM within a VM. I don't agree with rockeye on this one. There isn't a one-to-one relationship between V-VM-M. You're Models are designed according to business needs, with NO regard to presentation at all. You're ViewModels are designed according to your presentation needs, and may encapsulate more than one Model. In fact, it's very common for them to encapsulate many models.
Like rockeye, I don't understand your last question.
I am also quite new to mvvm, but i will try to answer :
Where the logic of loading a view dynamically with XAML reside
If you mean "how can i show the view associated with my business object?", IMHO, you don't have to care about this. Usually, your VMs have corresponding views. With dataTemplate, you use only VM in the code but Views are displayed automatically.
2 Whether i should have a single ViewModel for my WPF page or each seperate part have its own viewmodel with interactions with other viewmodel classes
It seems you have a top-bottom approach. I see the mvvm more as bottom-up : models (business objects) -> ViewModels -> Views. Every model should have its own ViewModel and view. So you can't have a whole WPF page in a viewModel unless you model represents a page.
3 What if i had to build control tree displayed on the GUI using C# code in the codebehind itself. For the controls created using code should i do the commandbindings in the codebehind of the view itself.
Don't understand. I think you may take a look at dataTemplate, it might be helpfull.

Resources