Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What contraints should I be aware of to maximize portability of Mono code?

I'm interested in writing some cross-platform code using Mono, with a view to targeting mobile iOS and Android runtimes.

I've perused the Mono and MonoTouch sites, but don't see anything that specifically advises on methods not to use, or Mono hooks which should be avoided. However, that seems a little too good to be true.

What limitations should I be aware of going into this project, to ensure maximum portability of the code?

like image 397
Marty Pitt Avatar asked Oct 04 '11 09:10

Marty Pitt


2 Answers

API wise you get a very similar base class libraries (BCL) when using MonoTouch or Mono for Android (M4A) since both share the same mobile profile (which was originally based on the Silverlight profile and enhanced to use more FX 4.0 API).

That's a lot of common code. Differences in the BCL are minimal but some do exists, mostly because running on iOS devices requires some compromises, which creates some limitations.

Outside the BCL both MonoTouch and M4A provides bindings for their platform. E.g. MonoTouch provides monotouch.dll which binds most of the iOS (C-based or ObjectiveC-based) API. That part won't work on Mono for Android (and the same is true for the Android bindings that M4A provides).

That's where you need to come up with a design to minimize the differences. In many cases the most different aspect is the user interfaces and there are a few approaches, many of them are based on MVC (e.g. MonoCross) to make this easier on developers while enabling a native look-and-feel for every platform.

like image 57
poupou Avatar answered Sep 28 '22 02:09

poupou


In addition to UI, get in the habit of ensuring that external dependencies have variants that function across all your desired platforms, or be ready to factor them out.

For me, the pitfalls were:

  • Compression - Not available in silverlight, user sharpziplib if needed (I know you're not targetting silverlight, but just in case.)
  • Serialization - Protobuf-net is your friend.
  • Storage - This is the trickiest. Are you using regular IO, or isolatedstorage, or one thing on one platform and something else on another? Also, mono.sqlite works just about everywhere except on silverlight, so go with that and have a contingency plan for silverlight, if needed.
  • Invented at MS - Forget about this stuff. WCF, entity framework, SQL-CE, LINQ to SQL, and on and on. Mono does the core .NET stuff perfectly, but is very patchy about the peripheral technologies, which IMO are not of much value anyway.

That being said, I found portability to be amazingly painless. In fact it was so good that I was able to move code intended for the desktop over to mobile in just a day or two, though after that I had to spend quite a lot of time optimizing. Just because code is portable doesn't mean that it will perform identically on all platforms! I think its best to do the green-field coding on the most performance-sensitive platform, which, IMO, is monodroid as that has the complications of JIT on a mobile device, cross-VM garbage collection, and so on.

like image 34
tempy Avatar answered Sep 28 '22 03:09

tempy