The “Skinny” on Reusing .NET Code in Mono
MonoTouch is a commercial framework from Xamarin, which allows you to write iOS applications using the C# language and a framework of classes that, although, pleasantly similar to the .NET Framework Base Class Library members, is internally based on Cocoa Touch native API. Additionally, Xamarin offers MonoDroid, a twin framework that wraps the Android SDK and allows you to program Android applications using C# and skills from the .NET Framework.
Suppose your company plans to port an existing iOS application to Android (or vice versa). If both applications are made using a Mono-based mobile framework, you could easily reuse up to 90 percent of the back-end code. While the iOS and Android SDKs are fairly different; and to a certain extent, push a different vision of the mobile device, Mono-based frameworks provide an abstraction layer and normalize the different APIs to the programming interface of .NET classes.
- It is much easier than dealing with native SDKs for many developers embracing iOS and Android programming.
- Using a single set of classes helps you gain reusability.
So, what are the limits to this approach?…
Let me put it this way, it’s NOT about true binary reusability and it’s NOT like in pure .NET or Java programming where you create external packages once and then link them to a variety of projects. In the mobile space, reusability refers to the ability of recompiling the same source code within the context of distinct projects for distinct platforms, which is not surprising considering that an iPhone and an Android application require different operating systems and possibly even different computers to be developed.
- You can’t create an iOS application without running under a version of Mac OS (you can create Android application running under Linux, Windows or even Mac OS). The best you can do, is to make a copy of the source files and recompile them across projects. Not much, compared to plain .NET or Java programming; but nevertheless, a milestone in mobile development.
- Reusability is limited to whatever code is invoked underneath the presentation layer. Just because every mobile application runs on its own operating system, it does not mean that the work of abstracting the underlying API is limited to providing tools for building the logic but not the user interface. E.g., if you have some segments of business logic, you can easily reuse them; if you have bits and pieces of application logic, you’re welcome to reuse the source code. That’s it. There’s no way to copycat, say, a MonoTouch application to Android (or vice versa).
Now, don’t get me wrong. It’s MUCH better to have this kind of limited chance of reusability than not! Furthermore, because it’s all based on a .NET-compliant programming interface, Windows Phone apps can be easily added to the list. So, when embarking on a journey in search of The Holy Grail of cross-platform mobile development, MonoTouch, MonoDroid and the C# language on the background, are a really great start.