Monday 25 December 2017

A Quick Guide To Optimizing Code Sharing & Reusability With Xamarin

An enterprise needs to focus on a myriad of factors when planning their cross-platform app development. From exploring simple ways to creating multiple versions of their app to making it unique and more usable, many enterprises consider Xamarin as the best cross-platform app development framework.



Not only does it help to build multiple versions of an app through a single codebase but also leverages C# to enable programmers to share the code across various cross-platform apps using three different techniques:

1. Shared Projects – This method uses the Shared Asset Project type to organize the source code alongside using #if compiler devices to manage platform-specific requirement when required.

2. Portable Class Libraries – In this method, you can create a Portable Class Library (PCL) to target the platform you want your app to support and use interfaces that provide platform-specific functionality.

3. .NET Standard Libraries – The .NET Standard Libraries work just like PCLs do. It uses interfaces to inject platform-specific functionality.

In this article, we will review all the alternative techniques for sharing code between cross-platform mobile apps while pursuing Xamarin mobile application development services. The goal is to help you choose the right project type for your cross-platform mobile apps. So, let’s get started.

Shared Projects:
Shared Project is the easiest approach to sharing code files. The picture below shows a solution file that contains three mobile app projects, each for Android, iOS, and Windows as well as a shared project that includes common C# codebase.




The following diagram shows the conceptual architecture where each project comprises all the shared source code files.




Advantages:
• Shared Project makes source code shareable across multiple app projects
• Shared code files can be branched depending on the platform using compiler directives
• App projects can contain platform-specific references that the shared code can utilize

Disadvantages:
• Shared Project doesn’t have an ‘output’ assembly, unlike other project types. At the time of compilation, the files are considered to be a part of the referencing project and compiled within that assembly. If you desire to share code as an assembly then PCL or .NET Standard Library is a better option.
• Refactoring operations that affect code within ‘inactive’ compiler directives do not update your code.

Portable Class Libraries:



Advantages:
• PCLs let you share code across multiple app projects
• Refactorings update all the affected references regularly

Disadvantages:
• PCLs do not use compiler directives
• You can only use a subset of the .NET framework determined by the profile selected

.NET Standard Libraries:


Advantages:
• .NET Standard Libraries enable you to share code across multiple app projects
• Refactorings update all the affected references regularly
• A massive surface area of .NET BCL (Base Class Library) is available in place of PCL profiles

Disadvantages:
• .NET Standard Libraries do not use compiler directives

Conclusion
The code sharing strategy you implement must be driven by the mobile platforms you wish to target. Choose the right method and ensure that it works best for your cross-platform app project. PCL and .NET Standard Libraries are best suited for building sharable code libraries while Shared Projects are ideal app developers planning to use a lot of platform-specific functionalities in their hybrid mobile apps. In case of any queries pertaining to this tutorial, reach out to us using the comment box below. 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.