What to do with all these platforms?

Developing sample applications becomes a bit of a hassle when you support as many platforms as Couchbase Lite .NET does.  However, there is a great library available (that I’m sure many people are already familiar with) that makes it far less painful.  In this post I will talk a bit about how I’ve used it, and how I will continue using it, to make my life easier when it comes to sharing code between platforms.

The project I made and will keep working on in the future is Grocery Sync.  This is not the only one, but I hope to consolidate all our sample apps to this one.  This project exists in the Couchbase Lite .NET repo but it was developed a long time ago and the code sharing is practically zero.  With this overhaul I’ve been able to put more effort into sharing and 100% of the logic is platform agnostic, while the current UI code is shared between iOS and Android thanks to Xamarin Forms.

The library in question that makes this possible is MvvmCross.  It is a quite mature and popular cross platform library to facilitate the use of the MVVM model of creating applications, and the developer is one of the more active that I’ve seen.  He has personally answered countless questions on Stack Overflow (including at least one of mine).  This is particularly poignant in this case because UI code is among the least portable out there and the model splits this out, making the rest of the code much more portable.  At a high level, it works by putting all the logic for a view into a view model, and then at runtime registering a way to present that view model.  There are tons of helpers in the library to make registration of view models, and their respective views, very easy.  Furthermore, nearly every step of the initialization process is customizable so you have complete control over what goes on.  This is only scratching the surface of what the library can do, but for now I haven’t gotten into more advanced use cases.

So here is how the project is set up in my case.  Couchbase Lite is not a portable class library so I opted to go with a shared project approach.  The GrocerySync folder contains all of the logic.  It’s not as clean as I’d like, and I make no use of models at all (the actual interaction with the database should be split out into the model layer) but everything in here is shareable between all the platforms I need (iOS, Android, WPF soon, and probably UWP in the further future).  The GrocerySync.Forms folder contains all the Xamarin Forms UI code.  This is another shared project because it needs to be shared between the iOS and Android projects (though this could have been a PCL, but that would have added a slight complexity to registering the views since they would be in a separate assembly from the view models).  Finally, the iOS and Droid folders contain platform specific boilerplate stuff as well as app icons, splash screens, metadata, etc.  This part, once set up, is rarely touched.

I need to add WPF once I am back to my Windows machine, but this should be trivial.  MvvmCross also has a WPF presenter, so all I need to do is write the UI for WPF, add the WPF boilerplate and use the existing GrocerySync logic that I wrote for the other two platforms.  Similarly, once UWP support makes its way into Couchbase Lite that process can be repeated.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s