The document summarizes Xamarin, an open source mobile application development platform. It discusses who the presenters are and their experience with Xamarin. It then provides an overview of Xamarin, explaining that it allows developers to build native cross-platform apps with shared C# code and access to full native APIs. The document also discusses the presenters' preferred Model-View-Presenter approach when using Xamarin, how they implement dependency injection, important Xamarin libraries, and the benefits of MVP including improved testability and code sharing. It provides examples of typical code reuse rates and evaluates Xamarin's performance in terms of app size, startup time, and potential downsides.
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Xamarin MVP Approach: Code Sharing, Performance and Best Practices
1. Xamarin in real life: our MVP approach
Antonino Cacace – Daniele Leombruni
ROME - APRIL 13/14 2018
2. { Who we are }
What’s Xamarin
Model View Presenter on Xamarin
Dependency Injection
Xamarin toolbox: must have libraries
Why MVP
Code sharing performance
Size performance
Startup performance
The dark side of Xamarin
3. Who we are
Daniele Leombruni
Mobile Architect for
Mobile & Creative Technology
at NTT Data.
iOS developer since 2015
Xamarin developer since 2015
Antonino Cacace
Mobile Architect for
Mobile & Creative Technology
at NTT Data.
Android developer since 2012
Xamarin developer since 2015
4. WeuseXamarin since2014 and in these
yearswe haveimproved our developing
processto obtain better performance
and quality.
Wedevelop apps for themost
important customersinenergy, media,
telco, manufactoring& retail sectors.
Who we are
5. Who we are
{ What’s Xamarin }
Model View Presenter on Xamarin
Dependency Injection
Xamarin toolbox: must have libraries
Why MVP
Code sharing performance
Size performance
Startup performance
The dark side of Xamarin
6. What’s Xamarin
Intro
Mono Framework
Open source development
platform based on the .NET
Framework, allows
developers to build cross-
platform applications
Xamarin Compiler
Compile C# code to IL
or to ARM assembly
code.
Visual Studio
Multi OS, powerfull
IDE
Xamarin.iOS and
Xamarin Android
Expose complete
native SDKs for .NET
developers
7. • Xamarinisamobiledevelopmentplatformto build,testandmonitoring cross-
platformapplicationsina fullynativemannerwith a singlecode base(known as
“Shared Code”)and with a single programming language (C#).
• ThemainXamarin’saim is to reusethe same shared code (about 40%)
acrossdifferentplatformsreducing significantlyboth:
• Developmentand Maintenancecosts
• Time-to-market
Xamarin apps have access to the full native spectrum of functionality exposed by the
underlying platform and device. 100% SDK mapping.
Native api access
Native performance
Native user interfaces
Xamarin apps leverage platform-specific hardware acceleration, and are
compiled for native performance
Xamarin apps are built with standard, native user interface
controls.
What’s Xamarin
Xamarin Native
Shared C#App Logic
Platform-specific C#Platform-specific C#
8. CompilercompilesdowntoIntermediateLanguage (IL),
which isthenJust-in-Time( JIT) compiled tonativeassembly
whentheapplicationlaunches.
Thereisalsoan experimentalversionof
*AOT compilerforAndroid.
Xamarin.iOS andXamarin.Android arebuilt on topofMono
Ahead-of-Time( AOT)Compilercompiles Xamarin.iOS
applicationsdirectlytonativeARM assemblycode.
Mono
AOT
Code
Mono
IL
JIT*
What’s Xamarin
What’s inside a Xamarin app
9. Who we are
What’s Xamarin
{ Model View Presenter on Xamarin }
Dependency Injection
Xamarin toolbox: must have libraries
Why MVP
Code sharing performance
Size performance
Startup performance
The dark side of Xamarin
11. *Or whateverView implementation(Fragment,Conductor, etc.)
MVP on Xamarin
Xamarin Implementation
IPresenter
INTERACTOR PRESENTER
UIViewController
*Activity
SharedLibrary
(Platformindipendent)
iOS/Android
(Platformdipendent)
The backbones of the application are the contract interfaces.
It’s essential to define it well from the beginning.
IController
12. Who we are
What’s Xamarin
Model View Presenter on Xamarin
{ Dependency Injection }
Xamarin toolbox: must have libraries
Why MVP
Code sharing performance
Size performance
Startup performance
The dark side of Xamarin
13. Dependency Injection, based on the
IoC (Inversion of Control) pattern,
allows accessing platform specific
features in Portable Class Library
(PCL).
Shared code depends on interfaces
resolved in native code using a
simple and lightweight dependency
injection implementation.
SERVICE
CONTAINER
Depencency Injection
How
SHARED CODE NATIVE CODE
<RESOLVE> <REGISTER>
14. Who we are
What’s Xamarin
Model View Presenter on Xamarin
Dependency Injection
{ Xamarin Toolbox: must have libraries }
Why MVP
Code sharing performance
Size performance
Startup performance
The dark side of Xamarin
15. Refit
.NET Standard 1.4
REST library
Refit, a REST client library,
that solves the problem of
making API calls
Xamarin Toolbox
Must have libraries
Rx.Net
Reactive porting for .Net
framework.
Rx is a library for composing
asynchronous, and event-
based code with
observables, and configuring
it via LINQ.
Conductor
Fragment alternative for
Android.
Provide a framework that
allows building view-based
Android apps and thus
completely avoiding the use
of fragments.
SharedC# AppLogic
Platform-specificC#Platform-specificC#
SharedC# AppLogic
Platform-specificC#Platform-specificC#
SharedC# AppLogic
Platform-specificC#Platform-specificC#
16.
17. Who we are
What’s Xamarin
Model View Presenter on Xamarin
Dependency Injection
Xamarin toolbox: must have libraries
{ Why MVP }
Code sharing performance
Size performance
Startup performance
The dark side of Xamarin
18. MODULARITY
RESPONSABILITY
XAMARIN
CODE
SHARING
Using MVP keeps logic layer away
from the graphic one: in addition
each component is strongly
cohesive, has a specific role and
responsability; code is highly
modular.
This structure simplifies unit test
phase: test design and execution
are relative only to logic layer.
Using Xamarin, where the logical
layer is shared between platforms,
increases the benefits of MVP by
extending them to the development
of apps for multiple platforms.
Why MVP
Benefits
SIMPLIFY
TESTING
19. Who we are
What’s Xamarin
Model View Presenter on Xamarin
Dependency Injection
Xamarin toolbox: must have libraries
Why MVP
{ Code sharing performance }
Size performance
Startup performance
The dark side of Xamarin
20. Code sharing performance
Average shared code in real enterprise projects
The statistics of code reuse on the Xamarin official site is around 40%.
Using our MVP Approach, maximise the reuse of code of 25~30%. This range of values depends
on the complexity of the app graphics.
7K 7K 8KRetail brand #2
Retail Brand #1 5K 12K 10K
18%
SHARED iOS Android
14K 34K 29KEnergie brand #1
9K 11K 10K
NOT
MVP
PA brand #1
5K 5K 9K
OUR
MVP
PA brand #2
30%
11K 18K 17KEnergie brand #2
#of codelines
27K
77K
30K
19K
22K
46K
21. Who we are
What’s Xamarin
Model View Presenter on Xamarin
Dependency Injection
Xamarin toolbox: must have libraries
Why MVP
Code sharing performance
{ Size performance }
Startup performance
The dark side of Xamarin
22. Size performance
Xamarin must incorporate
monoruntime: this implies that
the final dimensions of apk and
ipa, in Xamarin environment,
are greater than thesame builds
in native one.
23. XAMARIN
FORMS
~ 15 Mb
XAMARIN
ANDROID
~ 5.5 MbANDROID
NATIVE
~ 1 Mb
Size performance
Apk and ipa size
XAMARIN
FORMS
~ 10 Mb
XAMARIN
iOS
~ 6 MbIOS
NATIVE
~ 2 Mb
The graphics below show apk and ipa size of an «Hello world» project in native, Xamarin native, and
Xamarin Forms environment using «Link Framework SDK Only».
24. Size performance
Linker
Xamarin Linker is used in iOS and Android projects, to remove unused code from compiled
assemblies. This helps reduce the final size of the apk or ipa.
Linker has three options:
None/Don’tLink,meansthelinkerwon’tdoanything
SdkAssemblies Only/Link FrameworkSDKsOnly islimitedtobase classlibraries.
Sdkand User Assemblies/Link Alliseverything,includingyourcode.
25. Who we are
What’s Xamarin
Model View Presenter on Xamarin
Dependency Injection
Xamarin toolbox: must have libraries
Why MVP
Code sharing performance
Size performance
{ Startup performance }
The dark side of Xamarin
26. Average startup time of an app on
Android, Xamarin Android and
Xamarin Forms
Startup performance
Average startup time on Android
310 ms
45 ms
150 ms
23 ms15 ms
80 ms
27. Who we are
What’s Xamarin
Model View Presenter on Xamarin
Dependency Injection
Xamarin toolbox: must have libraries
Why MVP
Code sharing performance
Size performance
Startup performance
{ The dark side of Xamarin }
28. The dark side of Xamarin
Pros
C#
«One language to rule
them all»
C# is powerful and flexible,
general-purpose, object-
oriented programming
language. You don’t need to
know Swift, Java, Kotlin or
ObjectiveC(thank god).
Less code
«Less is more»
Less code mean less work,
less bugs and less costs.
Great performance
«Fast as native»
Xamarin app performance
are almost like native app.
29. Another layer of (possible) bugs.
Hard to integrate some libraries
(binding is not always easy).
Visual Studio (for Mac) is (still) not
mature
Community not big as native
communities.
The dark side of Xamarin
Cons
Chi siamo
Ho partecipato allo Xamarin Training nella sede Microsoft di Reading(UK)
Mobile & Creative Tech
Siamo Xamarin Partner
Mono – Progetto open source lanciato nel 2001 per permettere di utilizzare il .NET framework su sistemi linux based
Xamarin Compiler – Compila il codice C# in codice eseguibile da mono
Visual Studio – Potente IDE
Xam.ios e xam.and – porting sdk android e ios in c#
Declinazioni di Xamarin -> Native/Forms
Promesse di Xamarin
{SPIEGARE LA PCL}
{SPIEGARE TTM}
{Considerate che quando escono le nuove version iOS e Android,vengono aggiornati in anteprima (fate un check)}
Cosa c’è dentro un ipa o un apk
Ios mono – codice compilato ahead of time
Android – mono codice IL just in time
Utilizzando Xamarin da molti anni e su svariati progetti, nel tempo siamo arrivati alla conclusione che il miglior modo di lavorare con questa tecnologia (almeno per noi e in base alla nostra esperienza) è utilizzare l’archittettura MVP.
In questi anni di lavoro con Xamarin siamo giunti alla conclusione che il miglior modo di lavorare con Xamarin (per noi) è utilizzando l’archittettura MVP
Cos’è MVP ->
Come implementiamo l’MVP nella realtà.
Una parte imprescindibile della nostra architettura è la «Dependency Injection»
Attraverso la DI riusciamo ad implementare logiche specifiche della piattaforma nel codice condiviso.
Utilizziamo un’implementazione leggera di DI. Registriamo le implementazioni all’interno del ServiceContainer nel codice nativo e possiamo risolverle nel codice condiviso.
Xamarin ha un’ecosistema di libreria esterne abbastanza ampio accessibile tramite il package manager di .NET chiamato nuget.
Refit -> Porting di Retrofit (famosa libreria android) per la gestione delle chiamate REST.
Rx.Net -> Implementazione del pattern Reactive su C# che sfrutta anche la potenza di LINQ
Conductor -> Libreria per Android che sostituisce I Fragment e le custom View. Porting fatto da noi.
Conductor è una libreria leggera che permette di costruire applicazioni android View-Based. Conductor fornisce un wrapper alle View Android che ti permette di gestire il ciclo di vita in maniera complessa
{imagine simpatic}
Entrare sul progeweto piu velocemnete
Ma vediamo le performance
Linee di codice comuni mediante l’mvp fra 25% e 30% - prima 18%
{46K con riuso 12K line in meno}