Chapter 6. The Sharing Game

In this chapter, we will discuss one of the most interesting and important aspects of developing with Xamarin: cross-platform code sharing. We will cover the following topics:

  • The file linking technique
  • Portable Class Libraries
  • The pros and cons of each approach

Sharing and reuse

One of the advantages of using Xamarin and C# is the ability to share code across your mobile apps as well as other .NET solutions. The reuse of code can provide significant productivity and reliability advantages as well as reduce many of the long-term maintenance headaches that come with long-lived apps. That's great, but anyone who has been involved in software development for a long period of time understands that reuse is not free and not simple to achieve.

There are practical aspects of reuse; the question is, "Physically, how do I package my code for reuse?" For this, we can use one of the following three methods:

  • Share source code that can be compiled into multiple projects
  • Share Dynamic-Link Library (DLL) that can be referenced by multiple projects
  • Share code as a service that can be accessed remotely by multiple clients

There are also more strategic aspects; again the question arises, "How can I organize my code so that I can reuse more of it?" To solve this problem, we have the following options:

  • Create a layered approach so that data access logic and business validation is separated out of the user interface logic
  • Utilize interfaces and frameworks that abstract platform-specific services away from the reusable layers

In this chapter, we will touch on both these aspects of reuse, but primarily focus on the practical side of reuse. Specifically, we will present two different approaches to bundle up the code for reuse.

So, what parts of our code should we try and reuse? In the work we have done on the NationalParks apps so far, one obvious set of code stands out for reuse: the persistence code, which is the logic that loads parks from a JSON file and saves them back to the same file. In Chapter 5, Developing Your First Android App with Xamarin.Android, we moved towards a reusable solution by creating the NationalParkData singleton. In this chapter, we will demonstrate two different methods for sharing the NationalParkData singleton across both our projects as well as other .NET projects that might need it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.66.13