Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Develop Android/iOS App with Xamarin from A to Z.

How to Develop Android/iOS App with Xamarin from A to Z. Clean Architecture and Dependency Injection (Part 1)

You may have noticed the massive hype around different tools and frameworks for cross-platform mobile development. Such things like React Native, Ionic, Flutter might ring a bell to you (:sarcasm:).

None of this will be discussed in this article.

Instead, we’ll guide you through creating a cross-platform, natively-compiled mobile app using .Net Standard Library to build modules with shared code. We’ll also utilize some of the best practices of mobile app architecture and code structure — Clean Architecture and Dependency Injection.

In short, this guide will get you started with developing fast and cool-looking mobile applications for both platforms using Xamarin.

In the first part, we’ll elaborate on the preparation steps and app architecture basics.

Without further ado, let’s go!

Project prerequisites

  • Visual Studio 2017 Community Edition. You can download it from here: https://visualstudio.microsoft.com/ru/downloads
  • Mac computer. Or for Windows development — Mac computer accessible on the network, for remote compilation and debugging.
  • Be familiar with the concepts of Clean Architecture and Dependency Injection

Full list of system requirements for MacOS and Windows can be found here:

System Requirements - Xamarin

Packages used

To implement the app, we’ll need some third-party libraries (packages). In Visual Studio, packages are installed via NuGet — the built-in package manager for .Net platform, which we will get acquainted with a bit later in this tutorial.

Here is what we’ll need:

  • Autofac — dependency injection framework https://autofac.org
  • Refit — networking library https://github.com/reactiveui/refit
  • Newtonsoft.JSON — JSON parsing library https://www.newtonsoft.com/json
  • System.Reactive — .Net Reactive Extensions https://github.com/dotnet/reactive

Installing Visual Studio

You can refer to the official guide for Visual Studio setup instructions:

Install Visual Studio for Mac - Visual Studio for Mac

Xamarin approach to mobile development

When it comes to mobile development, we got used to thinking that the only choice to build an app is to use native languages, Objective C and Swift — for iOS, Java and Kotlin — for Android. Xamarin offers us a single language — C#, libraries and runtime that works across both iOS and Android. And all of this compiles natively as if you were using Kotlin or Swift.

To learn more about how it works under the hood please refer to the official documentation:

Introduction to Mobile Development - Xamarin

The great thing is that Xamarin documentation is quiet comprehensive and detailed, so all the small things are fully covered there.

Code Sharing

Roughly speaking, Xamarin provides us with a way to code a shared C# backend for all the platforms at once, Android, iOS, MacOS and UWP (Universal Windows Platform). Business logic, data models, view models, restful service calls, interaction with SQL databases — things that are common to almost every app — all of this can be shared to both platforms.

Xamarin offers three options:

  • Portable Class Library
  • Shared Project
  • .Net Standard Library

Lets have a quick overview of this options.

Shared Project

This is the simplest approach. The common code lives in the Shared Project.

In this way the three application projects are sharing the same source code (the C# files in Shared). Any edits to the shared code will be shared across all three projects.

Unlike most other project types, a shared project does not have any output (in DLL form), instead the code is compiled into each project that references it. This is illustrated in the diagram below — conceptually the entire contents of the Shared Project is “copied into” each referencing project and compiled as though it was a part of them.

The code in a Shared Project can contain compiler directives that will enable or disable sections of code depending on which application project is using the code, which is suggested by the colored platform boxes in the diagram.

A Shared Project does not get compiled on its own, it exists purely as a grouping of source code files that can be included in other projects. When referenced by another project, the code is effectively compiled as part of that project. Shared Projects cannot reference any other project type (including other Shared Projects).

Portable Class Library

When you create an Application Project or a Library Project, the resulting DLL is restricted to working on the specific platform it is created for. This prevents you from writing an assembly for a Windows app, and then re-using it on Xamarin.iOS and Xamarin.Android.

When you create a Portable Class Library, however, you can choose a combination of platforms that you want your code to run on. The compatibility choices you make when creating a Portable Class Library are translated into a “Profile” identifier, which describes which platforms the library supports.

Advantages:

  1. Centralized code sharing — write and test code in a single project that can be consumed by other libraries or applications.
  2. Refactoring operations will affect all code loaded in the solution (the Portable Class Library and the platform-specific projects).
  3. The PCL project can be easily referenced by other projects in a solution, or the output assembly can be shared for others to reference in their solutions.

Disadvantages:

  1. Because the same Portable Class Library is shared between multiple applications, platform-specific libraries cannot be referenced (eg. Community.CsharpSqlite.WP7).
  2. The Portable Class Library subset may not include classes that would otherwise be available in both MonoTouch and Mono for Android (such as DllImport or System.IO.File).

For compatibility details, follow this link:

Introduction to Portable Class Libraries (PCL) - Xamarin

.Net Standard Library

In this tutorial we will stick to .Net Standard library, which is the successor and the better version of Portable Class Library.

The .NET Standard Library is a formal specification of .NET APIs that are intended to be available on all .NET runtimes. The motivation behind the Standard Library is establishing greater uniformity in the .NET ecosystem. ECMA 335 continues to establish uniformity for .NET runtime behavior, but there is no similar spec for the .NET Base Class Libraries (BCL) for .NET library implementations.

You can think of it as a simplified, next generation of Portable Class Library. It is a single library with a uniform API for all .NET Platforms including .NET Core. You just create a single .NET Standard Library and use it from any runtime that supports .NET Standard Platform.

App structure overview

In this project we will make use of Clean Architecture pattern.

The main purpose of Clean Architecture is the separation of concerns. By keeping the business rules and data logic not knowing anything about the outside world we can make out app easy to test, maintain and increase code-sharing portion on the application.

We will also utilize Dependency Injection pattern to decouple object instantiation and get rid of creating and passing dependencies into their constructors manually.

Dependency Injection let’s us cut down on boilerplate code and make the development process more smooth. Implementing proper dependency injection in our apps allows us to have:

  • Loose coupling
  • Easily testable code
  • Code reusability

Our app will consist of two shared modules:

  • Domain
  • Data

and two platform-specific modules:

  • Presentation.Android
  • Presentation.iOS

Ta-daa! You’ve become familiar with the app architecture and preparation steps. As opposed to this episode, the next part of the article will be all about practice because we’re going to show you how to start your won project from ground zero and implement app layers.

So stay tuned for our next article and don’t forget to subscribe!

Also, if you’d like to know more about the technical side of things or want to develop your own profitable mobile app, feel free to drop us a line at [email protected]

How to Develop Android/iOS App with Xamarin from A to Z. was originally published in JetRuby on Medium, where people are continuing the conversation by highlighting and responding to this story.



This post first appeared on JetRuby Agency - Featured Technical Stories Based In Our Experience, please read the originial post: here

Share the post

How to Develop Android/iOS App with Xamarin from A to Z.

×

Subscribe to Jetruby Agency - Featured Technical Stories Based In Our Experience

Get updates delivered right to your inbox!

Thank you for your subscription

×