Episode I — Introduction to MVVM and MvvmCross

This is the first post of a series about MVVM. In this series I will talk about the MVVM pattern (Model-View-ViewModel) applied to an Android environment using Xamarin. In this first post, I will talk about the basics of Xamarin and explain some of the Framework that will be used in the examples, the MvvmCross.

The motivation for the creation of this content is the lack of sources on the subject and the lack of starting points for Xamarin as a whole. Following the Xamarin community (in StackOverflow and Monkey Nights Devs group, among other places) it is clear that many developers come from an Android background or simply unaware of the MVVM pattern, which is widespread in .Net.

The approach of this post is less formal than what you would find on StackOverflow, whilst also trying never to miss important details for your understanding. Now let’s get down to business!

What is MVVM?

Disclaimer — These are basic theoretical concepts for those who have never heard of MVVM. If you are already familiar with this pattern, you can skip this without losing anything.

Model-View-ViewModel (MVVM) is a software architectural pattern created by Microsoft engineers in order to facilitate the decoupling of UI and business logic responsibilities during the software development process. Programs written using MVVM are easily testable and maintainable. For more historical information about MVVM (which is not the purpose of this post), you can visit this page, from Microsoft

The way the architectural pattern works can be easily explained. The models are the representation of the data. ViewModels (VM) are abstractions that have the intention to expose only what is necessary for the Views (presentation layer) to display the data on the screen with ease. Thus, the data (models) are shaped to generate ViewModels that are used by Views to display the data on the screen. As simple as it may seem, this separation provides great benefits.

One of the benefits is testing. ViewModels are independent of user interactions. This means that writing unit tests for them is simple (and very wise). This brings us to another crucial point for using MVVM with Xamarin: the ViewModels are independent of Views. Ideally, ViewModels are unaware that a view exists, so the same ViewModel can be used by more than one interface or platform!

“Cool, a VM can serve many Views. So what?” You ask. The answer is: That is exactly the what Xamarin is for! Reusing the same code to generate apps for most platforms. That is, a ViewModel written in C # can control Views that will be used on your Android, iOS and Windows Phone apps! Wow!

And how does it all work with Xamarin?

It is important to remember that MVVM is just a architectural pattern. We need a framework that implements this pattern to put it into practice. There are numerous MVVM frameworks available. The one we are using for this series is MvvmCross, a comprehensive framework that works very well with Xamarin. However the concepts of MVVM you will learn, are applicable to any other framework.

When using MvvmCross, you’ll need a Core project, where all the shared logic between platforms will reside (ie ViewModels and Models, Network Calls, Interfaces, etc) and a specific project for each platform. Thus, a project that will work on Android, iOS and Windows Phone will have at least 4 projects.

To use MVVMCross is super simple. Just install the MvvmCross.StarterPack package in each one of your projects and it will create the minimal structure to start your project. The subject of the next post will be precisely this: Understanding the structure of a MvvmCross app.

Do I have to pay anything?

Not anymore, MvvmCross is 100% free and open source. You can look at the code, modify it and use as you want without paying anything. Since Xamarin acquisition by Microsoft, the platform is now free you can start by downloading Xamarin Studio Community or Visual Studio Community from this link.

In our next post we will discuss the structure of a MvvmCross app. Stay tuned.

Thanks to willsb for the original post in Portuguese.