A Badge of Awesomeness
Steve Dunford
By Steve Dunford

Tagged:

MVVMRules

MVVMCross for cross-platform mobile development.

If you’ve landed on this blog post after a brief click around the Sequence site, you might have noticed that we’ve recently done some work for the British and Irish Lions, producing an app for the 2013 tour that is available on seven different platforms. When we first sat down to work out how we were going to build the app, we looked at the requirements, the number of platforms and our time scales and realised that we had a bit of a problem. To build a native app for every platform was going to take time – time that we didn’t have. And we didn’t think Warren Gatland was going to push back his squad announcement for us. Not even if we asked him nicely.

So we had to look at how we could build the Lions apps in a cross-platform way, reusing as much code as possible. We looked at the options – PhoneGap, Adobe Air, Titanium etc., but although these would have been quicker than writing native apps, they either didn’t give us enough access to native functionality and controls, didn’t give us the performance we would need or wouldn’t allow us to achieve the desired quality of finish within the available timeframe.

Instead we decided to plug for a full .NET solution, consisting of a PCL (Portable Class Library) common core plus platform-specific UI layers, using Xamarin on iOS and Android. With this architecture, anything that was common to all platforms could go into the core and be written and maintained just once, saving us time and money.

To write an app this way, we needed an architecture that achieved maximum separation of concerns between our platform-specific UI and our common business logic, whilst keeping the UI layer as lightweight as possible. We’d previously written Windows 8 and Windows Phone applications that used the MVVM design pattern to do just that, and so what we wanted was a way to use the MVVM pattern in a cross platform application.

Happily, there is one

MVVMCross is an open-source framework written in .NET and Xamarin that allows you to use the MVVM design pattern across all platforms. Models and view models sit in the PCL core, and then you use data-binding to populate your platform-specific views from the common view models. In Android and iOS, MVVMCross provides a binding mechanism for you to do this – in Windows you can just use the inbuilt one. So, your UI projects basically just consist of your views (XAML in Windows, AXML in Android etc.) and some supporting code to do value conversion and platform-specific jiggery-pokery like Live Tiles or Share Charms. Everything else can go in the core and just be written once. And that’s pretty cool.

Another nice thing about MVVMCross is that it comes with plugins that implement commonly-used functionality on each platform and then abstract it in such a way that you can call it from your core without caring what platform you’re running on. For example, it comes with a SQLite plugin which lets you read and write data to a SQLite database from your common code. The actual code that gets run will depend on which platform your core is running on, but MVVMCross takes care of that – you just integrate with it once. You also get plugins for stuff like opening a web browser, File I/O, geo-location and sharing. 

Since we wrote the Lions apps there’s been a new major revision, with new cool stuff being added all the time. Plus, the whole framework is put together using IoC and Service Orientated Architecture, so if you need to do something non-standard or to extend the framework in some way, it’s easy to do. We found, for example, that we needed to tweak the way image caching works within MVVMCross, and that we could do so just by providing our own implementation of one of the core services and instructing MVVMCross to use our modified implementation instead of the default.

Go, Use It

Overall, Xamarin and MVVMCross saved us stacks of time on the Lions project, and enabled us to deliver great quality apps within a tight timescale. As with any framework we encountered issues and bugs along the way – but because MVVMCross is open source we were able to either fix or work round issues we encountered. We worked closely with Stuart Lodge who wrote and maintains the framework; we even had him on site working with us for a few weeks, during which all of us on the Lions team learned a lot about the inner workings of MVVMCross and consumed rather more doughnuts than any human being is meant to in a given 24-hour period.

Along the way, it was my privilege not only to use MVVMCross but also contribute back to it – something that we at Sequence are keen to do whenever possible. For my efforts I was rewarded with a fabled and revered (albeit by a select few) MVVMCross Badge of Awesomeness. But we think that MVVMCross is awesome, and so, if we could, we would make our very first Sequence Badge of Awesomeness for Stuart Lodge and MVVMCross. Go use it. It rocks.