[Guest Post] On Cross-Platform Mobile Development With C# and Mono

This is a guest post written by Mike Fayer, the developer of Kleio Flashcards.

These days, most of the mindshare in cross-platform mobile development is taken up by HTML5/js toolkits such as Titanium. There’s a common argument, with which I happen to agree, that HTML5/js will never produce as “native feeling” and snappy an experience as, well… the native experience. But just because HTML5/js isn’t (currently) up to the task, that doesn’t mean there isn’t a better way to do x-platform mobile development.

That’s where Mono comes in. Mono is an open source implementation of the C# language, runtime, framework, and compiler infrastructure. It also allows execution of C# on Android and iOS via Mono For Android and MonoTouch – proprietary but decently priced products derived from Mono. They’re made by a company called Xamarin, which also acts as a caretaker of the Mono project as a whole. With these products, one body of C# code can reach across iOS, Android, WP7, Windows Classic, Windows 8 Metro, Linux, OS X and a sizable handful of more obscure platforms (NetDuino, anyone?). MonoTouch has been around for a while, has an active and established community around it, and many apps on the marketplace. MonoDroid is younger (just barely a year) but its starting to gather steam and, in my experience, is more than ready for prime time.

There are two reasons that Mono apps feel like native apps. One – Mono executes at comparable speed to native code on iOS and on Android. Two – and this is a feature, not a bug – there is no write-once, run-anywhere UI. And there is no reinvention of the UI system. Instead, Mono hooks into the native UI system of each platform. This means that for each platform, you have to rewrite the UI from scratch. There’s no better way to get a “native” feel on a cross-platform app than to use the UI system that was developed specifically for that platform, and also to respect the various patterns, conventions, and lifecycles that come with that system. In fact, what Mono does is provide C# bindings to a large portion of the native framework on whichever platform you’re working on – more than just the UI system. 

The challenges of writing a Mono app are the sum of the systems being targeted. The code that can be reused has to be generalizable from the platforms and the patterns that those platforms employ, code that fails this test cannot be reused. Its up to the developer to find an architecture that will produce the most code reuse while still working on every platform. With a proper design, it should be possible to reuse the model, the data persistence layer, any serialization, the remote communication stack, and a lot of the business and execution logic. All code relating to the native UI systems and its patterns must be written from scratch on each platform. I see this as a plus, because it obliges the developer to develop the sort of familiarity with each platform that’s necessary for a truly excellent app. This is not write-once, run-anywhere, but in my experience the roughly 70% of code reuse that you can get is well worth the effort. This is particularly the case when you consider the reduction in testing and debugging efforts that result from not maintaining 5 different versions of the same app.

The greatest difficulty of using Mono lies in bridging the various architectural patterns of the mobile platforms. iOS has its particular flavor of MVC, Android has its highly independent Activities and Services glued together by intents, and the Microsoft platforms all use MVVM. Kleio Flashcards, my app, uses a hybrid home-rolled MVC architecture, with the View layer encapsulating the idiosyncrasies of the particular platforms (so on WP7 – the view layers contains both views and viewmodels), and the Model and Controller layers are reused across the different platforms. There are currently efforts to bridge the architectural gaps (see the very promising ‘MvvmCross’ project) from both the Mono community and from Xamarin, so I expect this weakness to be addressed in the near future. Another difficulty comes from using open source libraries on Android and iOS in Mono. Xamarin is also addressing this with tools that make it possible to bind arbitrary libraries into Mono. Its not always smooth, but it works – and the MonoTouch/Droid communities are growing by the day and producing their own open source libraries.

With the rise of WP7 and the arrival of Win8 and WP8, lots of developers with existing .NET experience are about to get a whole lot of new consumers as potential users. And, as more and more mobile apps take on social and community aspects, I think that it’s incumbent upon us developers to not arbitrarily divide our users and their real-world relationships with platform-determined walls. Covering multiple platforms is good for our businesses, and its good for our users. Mono is – in my opinion – the right technology at the right time to make this happen.

Are you interested in taking part in Developer Appreciation Month? Let us know by contacting us with the subject “Developer Appreciation Month”. We’d love to feature your app.


Comments are closed.