Friday, October 1, 2010

MVVM Overview (part 2 : Model,ViewModel,View, ... what)

The Model is simply your business or data layer. It's completely UI independent.So when you develop for example a WPF application,
and you would like to switch it to a silverlight application later on, you can just re-use your Model.

Your ViewModel exposes the data to your View. So it's 'custom' build to show the data for your View,
even though your ViewModel acts independently from your View, and your View is developed independently from the ViewModel.
So they are in a way 'connected'.

Your view just connects to the data with databindings. They can be one way bindings (property to source or the opposite) or two
way binding. I'll give examples of that later on, basically it means that if you change something on the screen, if it gets
changed in your properties too, or only your property/screen changes.

MVVM overview (part 1 : What is MVVM)

So what is MVVM and what benefits are there in using it?

MVVM is a design pattern that is a variation on the MVC (Model/View/Controller) pattern.
MVC pattern seperates data model (Model) from the UI (View) and the application logic (Controller).
In MVVM there are also 3 layers: The Model (data model), the View (UI) and the ViewModel (Exposes the data from the Model
to the View), more on these layers later.

In modern applications this gives a significant advantage: The UI can be completely made by a designer, without him getting in touch with
the coding part of the application. (useful for Blend!)

Another advantage is testability, because the layers are seperated the ViewModel can easily be unit tested.

The databinding in WPF really makes this pattern shine, I'll show some examples later on.

So what are the disadvantages?

There are none! No seriously now, I don't really think there are major disadvantages in using MVVM pattern in WPF.
Maybe the fact that for some stuff (like for example showing a dialog box) .NET lacks in support, but there are MVVM frameworks that
can you help with these problems, and a good community searching and discussing solutions to these problems.