Wow – how’s that for the longest title ever for a blog post?

What is “Code Sharing”?

When I say the words “code sharing”, I’m talking aboutcreating a library that can be used across all platforms that Xamarin supports (and even some they do not). In other words, a single librarythat can be shared across aniOS, Android, Windows Phone, and an OS X application. To make things even better, when properly done, wedon’t need to do anything special to share the code across the platforms. All weneed to do is import the library into thesolution and it will work. There’s no need to write code and compile especially for Android and then turn around and make code changes and recompile for iOS – we just need to write and compile the library once! That’s pretty cool – no wonder I acted like this guy when I found out about this!

CelebratingHipster

When to Code Share?

There are certain scenarios where code sharing across platforms is appropriate (and useful) and others that sharing code does not make sense. Let’s start with the scenarios where we’ll get the most bang for our buck with code sharing.

Business Logic

This is business logic in the traditionalsense of the term, for example, model classes that represent the core entities of your application. The classic example being a customer class that models the name and address of a customer. Then any manipulations to those models, like a customer class being able to place an order, are part of the business logic that can be shared across platforms.

REST/Web Services

This one is really pretty cool. Within the shared code, we can invoke a web or REST based service and parse the return values. Previously I had always assumed that accessing the Internet was dependenton the operation system or device. But that’s not the case – we can access the Internet, parse return values (JSON, XML, pretty much anything) all within a shared code library.

Data Access Logic

Any code which controls how or when the models access the database is appropriate to put into a shared code library. This would include implementing security on certain tables or restricting certain business logic functionality from writing to tables. We can limit “who can do what” to the database in the shared code layer.

Database Engine and Persistence

At first glance this one seems impossible – not only can we have the code which persists to the database in a shared library – but the database engine itself is sharable across platforms! By using SQL*Lite, we can have the same database engine and the same code which manipulates that database all in a shared, cross platform library. I can’t tell you how much time this has saved me across multiple projects.

When Not To Share Code

Of course, there are situations where sharing code is not possible, and all of these revolve around platform-specific and device functionality.

User Interface Elements

The actual elements which the user interacts with on the screen are not sharable across platforms. Things like buttons, list views, even text boxes are implemented fundamentally different across operating systems. Of course, things like Xamarin Forms make it possible to write a user interface once using XAML and then have the toolset create a user interface for each operating system. But even in that case, if you wanted to implement somethingoutside ofthe base functionality, you’ll have to write a custom renderer for each platform.

Here we’re talking about how the user of an app navigates forwards and backwards within an application. The steps the user performs in iOS with the soft back button at the top of the screen or using the swipe right gesture is completely different on how a user of an Android application would go back with the hardware back button. Any gesture based mechanisms also cannot be shared across platforms. For example, if wewanted to create a new 4-finger long hold gesture recognizer – we’dhave to implement it both on iOS and Android separately.

Device Specific Functionality

When we want to access the camera or location based services on the device we won’t be able to do that through code which is shared across platforms. Checking which device features (such as a camera) are available is done in an operating system specific manner – thus not shareable.

In other words – anything that deals with the visual or tactile user experience or accesses functionality that’s specific to a device is not a candidate for a single code library between platforms.

Next Steps

So far we’ve covered what exactly code sharing means, situations when it makes sense to share code, and some where sharing is not feasible. This is really cool stuff – one library – works everywhere … pretty close to the “write once, run everywhere” nirvana!

In the next couple of posts in this series, we’ll tackle some methods of implementing the shared code library, and we’ll even get to use 3rd party packages from NuGet!