The “Skinny” on Reusing .NET Code in Mono

ago 1, 2012 by     1 Comment     Posted under: Mobile Solutions




















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.

What does that mean in terms of code reusability?

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.

The “Pros”

  • 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.
Warning:  I’m not saying that iOS and Android programming (even with Mono-based frameworks) is a walk in the park.  However, MonoTouch and MonoDroid, definitely make it simpler and much more approachable.

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.

The “Cons”

  • 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.

 

Dino Esposito

CTO of a company that provides software and mobile services to professional sports, Dino is a well-known ASP.NET expert who has written several popular books, including Microsoft .NET: Architecting Applications for the Enterprise, Programming Microsoft ASP.NET, and Programming ASP.NET MVC, all for Microsoft Press. His latest book, Architecting Mobile Solutions for the Enterprise has just been published by Microsoft Press and is available for sales.

More Posts

Follow Me:
Twitter

1 Comment + Add Comment

  • I have seen lots of useful points on your site about computers. However, I’ve the view that netbooks are still more or less not powerful adequately to be a good option if you normally do projects that require lots of power, like video modifying. But for world wide web surfing, microsoft word processing, and a lot other popular computer functions they are just fine, provided you do not mind the screen size. Thank you sharing your opinions.

Got anything to say? Go ahead and leave a comment!