27
Enterprise Xamarin Tips and Tricks Robin Schroeder @RTSchroeder [email protected]

Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

Enterprise XamarinTips and Tricks

Robin Schroeder

@RTSchroeder

[email protected]

Page 2: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

Why am I qualified to talk to you?

Robin Schroeder

• Coding since 1998 - VBA, Java, VB6, SQL, web applications

• Started C# in 2008, Win 8.1, and UWP

• Full Stack - Mostly Middle and Front-End, Backend Azure

• Currently specializing in Xamarin mobile, UWP, iOS and Android

• Photography, Guitar, 3D Printing, Hololens, Unity, etc

• Fox Valley Girls Coding Club

“Being a small shop, we have thrived by learning how to do more with

less. We share our techniques that leverage code generation and inspire

others to rethink their approach to software craftsmanship.” -Paul

Page 3: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

What are we going to talk about?• Tooling

• Mobile Data Storage & GUIDs

• Sample Project – Bingo Buzz

• Cross Platform Mobile Architecture (Xamarin Specific)

• Dependency Injection & Platform Specific Code

• Memory Management

• Enterprise Plugins

• Authentication

• App Center: Analytics, Metrics, Crashes & CI/CD

• Application Insights and Beyond

• App Store vs Enterprise Deployments

• Where to read more…

Page 4: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

Tooling• Development

• Visual Studio 2017 on PC

• Visual Studio for Mac (MacBook & MacMini)

• XCode

• Telerik Fiddler / Orchestra (www.telerik.com/fiddler)

• SQLite Expert (www.sqliteexpert.com)

• Postman (www.getpostman.com)

• Code Maid VS Extension for PC

• Vysor (Android) and Reflector (iOS)

• Visual Studio App Center Account

• Apple Developer Program Membership

• Azure Account

• SQL Server Management Studio

Code Generation• Reverse POCO• CodeGenHero

Page 5: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

S.O.L.I.D.

S – Single-responsibility principle

O – Open-closed principle

L – Liskov substitution principle

I – Interface segregation principle

D – Dependency Inversion principle

Page 6: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

Two Mobile App Strategies for Data Storage

Long term data storage (SQLite, etc.)

Data connection not required – Offline CRUD

Generally not sensitive information

PK are generally GUIDs

Client DB schema usually mirrors server DB

Great candidate for Code Generation

No long term storage

Data connection required

Security sensitive information

PKs aren't important

DTOs don't match source DB

OK candidate for Code Generation

Page 7: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

Capturing Data with GUIDs

Multiple Devices

Running SQLiteOne Central

Cloud DB

Use GUIDs as the primary key datatype

GUID = Global Unique Identifier

UUID = Universally Unique Identifier

72468510-244a-4af5-bd8e-f8820c211b6c

Page 8: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

Sample Project: BingoBuzz

https://github.com/MSCTek/CodeGenHero/tree/dev/src/Samples/BingoBuzz

Page 9: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

What the heck is BingoBuzz?

Virtual meeting bingo game!

• Game is played as a virtual meeting is occurring

• All players have a randomized game board

• Players tap squares matching meeting events

• All phones 'buzz' when someone gets bingo

• Scores and stats are persisted

• Basically, it makes boring meetings fun

Technologies:

• Xamarin.Forms client - iOS, Android & UWP

• WebAPI and Azure SQL DB

Page 10: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

BingoBuzzhttps://www.dropbox.com/s/h4i9csvoem6pwtc/BingoBuzzDemo.MP4?dl=0

Page 11: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

So…Where do the biz rules go?

• .Net Standard 2.0 Xamarin.Forms

• Resources for shared assets

• Controls for shared UI pieces

popups, header, icon label

• ModelData for SQLite

• Helpers for high level biz rules

• ModelObj for more specific biz rules

These are partial classes

• ViewModels for INPC & MVVM

Only minimal biz rules here

• Views for UI

No Biz rules here

• Services, Interfaces & Modules for DI

• Generated CodeNo generated code in the platform specific projects

Handwritten partial models kept in Extensions folder

CodeGenHero configuration has its own project (bottom)

Page 12: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

WebAPI

DTO

Data Transfer

Objects

Object Model

INPC

ViewModel

ViewWHY?

Serializable

In and out of

JSON

WHY?

Storable in

SQLite

No children

DataModel

SQLite

WHY?

Object ModelINotifyPropertyChanged

map map

MVVM Mobile Data FLOW

All your model are belong to us

Page 13: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

Service Layer Data Flow

DTO <-> DataModel

Demo Data

DataLoaderService

WebAPI

DataLoaderService

SQLite

DataModel

DataModel <->ObjModel

Unit Tests

Mock DataService

ViewModels

DataService

DTO -> DataModel -> ObjModel

IDataLoaderService

IDataService

Page 14: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

What Can Be Generated?

DTO <-> DataModel

Demo Data

DataLoaderService

WebAPI

DataLoaderService

SQLite

DataModel

DataModel <->ObjModel

Unit Tests

Mock DataService

ViewModels

DataService

DTO -> DataModel -> ObjModel

IDataLoaderService

IDataService

Page 15: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

Dependency Injection

Graphic From: https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/hands-on-labs/aspnet-mvc-4-dependency-injection

public MyViewModel (IDataService iDS, IAnalyticsService iAS, ILoggingService iLS,

IPermissionsService iPS, ILocationService iLocS)

{

//Now I have an instance of each of the services I need to make a new viewmodel

//and the specific type of that service can vary based on my platform or test

}

Page 16: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

Platform Specific Code

FRONT END: The client

wants the text fields look

exactly the same on all

three platforms: yellow

background with purple

border and green text.

How do I do that???

BACK END: I need to store my SQLite database in a different place on each

platform specific file system,

but the db connection needs to be used by the

shared business logic.

How do I do that???

Platform Specific Services

Custom Renderers

Xamarin Effects

SHARED CODE: I need to hide the Bluetooth printing button in my app on UWP, but show the same button

in iOS and Android.

How do I do that???

Detect the OS in a conditional

Page 17: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

Memory Management• New is Glue

• In XF, we worry about:

• Apps that use lots of pictures, videos, pdfs, data

• Apps that will be open all day

• Worry most about Android, less about iOS, not at all about UWP

• Worry some about Xamarin.iOS outside of XF

• Xamarin Profiler – stand alone app

• Build/run your iOS or Android app, then run it again in Xamarin Profiler

• Take snapshots to see improvements between builds

• Works on Visual Studio Enterprise 2017 on a PC or Visual Studio for Mac

• Version must be kept in sync with VS

• Alternatives:

• Instruments for iOS

• DI Memory Reporter Service (like the one in Bingo Buzz)

• Use Diagnostic Tools in VS for UWP (if you must)

“The point of "New is Glue" is not

to say that "new is bad", but

rather to raise awareness. You

should think about the fact that

you're tightly coupling the current

function or class to a particular

implementation when you use the

new keyword. In many cases,

after briefly considering this, you'll

conclude that you're fine with the

coupling and continue on. But in

some cases you may realize that

you're about to add tight

coupling to an implementation,

and the current code isn't where

you want to make that decision. ”

@ardalis

Page 18: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise
Page 19: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

App is loading

User entering

Welcome page

Game board

Welcome page

Statistics page

Welcome page

Game board

Welcome page

Allocations on only live objects!

Page 20: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

Plugins & Components

Be Careful with Plugins in Enterprise Applications!

• Required dependencies?

• Is the Nuget in Preview? (exception MSAL)

• When was the last time this plugin was updated on Git?

• Do I know anything about the author?

• What are the git issue history related to the plugin?

• Does it make more sense to learn from the open source code and bake only the functionality I need into my project?

• 3rd Party Components - Syncfusion, Telerik, Mr Gestures, Infragistics, etc.

• Major Exceptions

• James Montemagno’s Xamarin.Essentials will probably always work

• Xamarin.Android Support libraries – don’t update until you are absolutely forced

Page 21: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

Azure Active Directory B2C

Azure Active Directory Business to Consumer

Same system that handles Office 365 – high availability and secure.

Free up to 50,000 users. Can use Facebook, make a local account, etc.

Great documentation. You can customize the UI. If you don’t want to use it

for production, it works well for Dev and QA.

https://azure.microsoft.com/en-us/services/active-directory-b2c/

Page 22: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

CLIENT LIBS: MSAL & ADAL

• MSAL – Azure AD v2.0: MicroSoft Authentication Library

• technically in “production-ready preview”

• Supports industry-standard OAuth 2.0

• Supports OpenID Connect 1.0 protocols

• ADAL – Azure AD v1.0: Azure Active Directory Authentication Libraries

• Older libraries

https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-libraries

Page 23: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

App Center is Great

• Build – Works great for iOS and Android, no so much for UWP

• Test – Xamarin Test Cloud = Awesome

• Distribute – Great for QA and Enterprise Distributions

• Diagnostics – Crashes and errors with stack traces

• Analytics – Events and real time log flow – you can pipe these into Azure

Application Insights and then on to Power BI

• Push Notifications – To a group or an individual (via InstallationId), very easy

to set up, silent (ios only) and non-silent (ios and droid), toast notifications in

UWP – huge improvement over doing this for each platform.

Page 24: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

Application Insights

• Everything is recorded as a Custom Event in

Application Insights

• Need to export if you want to save more than

60 days of analytics data

• Application Insights can dump into Power BI

easily

union customEvents| where timestamp between(datetime("2018-10-13T00:00:00.000Z")..datetime("2018-10-14T00:00:00.000Z"))| summarize Sessions=dcount(session_Id) by bin(timestamp, 1h)| order by timestamp asc| render barchart

Page 25: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

Enterprise Deployments

Page 26: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

App Stores• iTunes:

• Requires a real person reviews the app

• Expect to be rejected the first time for something minor

• You will need to create a production user and include credentials with app submission, this can sometimes be tricky when promoting code into prod and waiting for the store to review your app

• Hard to nail down exactly when the app will be ready for release – allow 4-5 days to be safe. Minor resubmissions can usually bedone in 24-36 hours during the week.

• Allow another 12 hours to be fully indexed in the iTunes store search

• Expect that your app size will DOUBLE once the build hits the store!

• Expect that you will not be able to modify app metadata without a store resubmission

• $100 a year, D&B number required for companies

• Google Play:

• Digital review, takes about 5 minutes, no human required

• Has really cool built-in tools for submitting potential release candidates so you can test in parallel

• Lots of tools for sifting through the thousands of android devices and deciding which ones your app should be available on

• Easy to modify the metadata for your app

• $25 one time payment

• Windows Store

• App in the Windows store that you run against your release build that catches problems

Page 27: Enterprise Xamarin - WordPress.com€¦ · •Cross Platform Mobile Architecture (Xamarin Specific) •Dependency Injection & Platform Specific Code •Memory Management •Enterprise

Where to read more…