Chapter 3. Code Sharing Techniques

In the last chapter, you built simple applications for iOS, Android, and Windows Phone. Even though each provided identical functionality, they were implemented completely independently of one another. This works fine for a simple application with one button and two screens, but is certainly not ideal for real world applications.

As mentioned in Chapter 1, one major benefit of the .NET approach is being able to share a lot of your non-UI code across all platforms. At first, you might think that almost all behavior in most applications is tied to the user interface in one way or another, which would make it very difficult to share any code across platforms. While this may seem true at a higher level, if you dig a little deeper, you’ll find it is often not the case.

For example, consider an application that accesses a web service to download its data in XML format, and then transforms that XML into some model classes that the application can use. All of this code is actually independent of the presentation layer and can therefore be shared across each platform (see Figure 3-1). You only need to write the code for the web service client, XML processing and model classes once, but get to reuse them everywhere.

It’s easy to see how this can apply to many other classes of functionality as well, such as libraries that perform calculations, process data, and process data. In many cases, you will find that the code that can be shared is often the real core ...

Get Mobile Development with C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.