azure mobile apps with xamarin

55
Dan Hermes Xamarin MVP President, Lexicon Systems @danhermes Azure Mobile Apps with Xamarin

Upload: danhermes

Post on 14-Apr-2017

246 views

Category:

Mobile


3 download

TRANSCRIPT

Page 1: Azure Mobile Apps with Xamarin

Dan HermesXamarin MVPPresident, Lexicon Systems@danhermes

Azure Mobile Apps with Xamarin

Page 2: Azure Mobile Apps with Xamarin

About me

Software consultant since 1999Code, write, and lead teams Minecraft, tiki cocktails, my parrot, and digital artI love Xamarin

Page 3: Azure Mobile Apps with Xamarin

“This weighty book gives clear guidance that will help you build quality apps, starting with architectural considerations, and then jumping into practical code strategies.” - Bryan Costanich, Vice President, Xamarin

“Dan Hermes’ extraordinary book is the most intelligent work on cross-platform mobile development I’ve seen.”

– Jesse Liberty, Director of New Technology Development, Falafel Software, Xamarin Certified Developer / Xamarin MVP

Page 4: Azure Mobile Apps with Xamarin

Book Tour

Page 5: Azure Mobile Apps with Xamarin

Book Tour

Page 6: Azure Mobile Apps with Xamarin

Why are we here?

Page 7: Azure Mobile Apps with Xamarin

We Apps

Page 8: Azure Mobile Apps with Xamarin

Everybody Apps!

189Mdownloads

a day

200mins on phone

127mins in

apps

Page 9: Azure Mobile Apps with Xamarin

The average app user has 36 apps installed on his or her phone.

Page 10: Azure Mobile Apps with Xamarin

Only 1/4 are used daily:

Page 11: Azure Mobile Apps with Xamarin

1/4 of apps are never used!

Why not?

Page 12: Azure Mobile Apps with Xamarin

Bad App Experiences

• Slow or laggy• Crashes• Unintuitive & bad UX• Features not as advertised• Data not available when you need it

Page 13: Azure Mobile Apps with Xamarin

Your app craves connection more than

you do.

Page 14: Azure Mobile Apps with Xamarin

http://opensignal.com/coverage-maps

Page 15: Azure Mobile Apps with Xamarin
Page 16: Azure Mobile Apps with Xamarin

No app is an island

Page 17: Azure Mobile Apps with Xamarin

Every app needs a server…DatabasesServices

AuthenticationNotifications

Files

…in the cloud, of course

Page 18: Azure Mobile Apps with Xamarin

Plenty of Options

Amazon Web Services

Azure Mobile Apps IBM MobileFirst

Oracle Mobile Cloud

SQLCIipher

Couchbase Realm

SQLite-net

Page 19: Azure Mobile Apps with Xamarin

• Extremely powerful• Flexible• Easy Tables• App Service

• C# SDKs available everywhere:• C#- iOS, Android, & Windows with Xamarin• C# clients, written by C# developers (open source)• C# backend with ASP.NET

Why Azure?

Page 20: Azure Mobile Apps with Xamarin

What is Microsoft Azure App Service?

Data storage with Azure SQL DatabaseAPI creationAuthentication and AuthorizationPush NotificationsJob processing

A platform-as-a-service (PaaS) offering of Microsoft Azure A ready-made backend so the developer can focus on the

app

Page 21: Azure Mobile Apps with Xamarin

What are Azure Mobile Apps?

Storage

AuthenticationPush

Courtesy of Microsoft

Page 22: Azure Mobile Apps with Xamarin

Storage, Authentication, and Push Notifications

Courtesy of Microsoft

Page 23: Azure Mobile Apps with Xamarin

How Do I Use Azure App Service?Azure Portal for configuration of entitiesServer SDK in C# or Node.JSClient SDKs for Xamarin and third-party products

• Azure Portal for configuration of entities• Server SDK in C# or Node.JS• Client SDKs for Xamarin and third-party

products

Page 24: Azure Mobile Apps with Xamarin

Let’s add a backend

Page 25: Azure Mobile Apps with Xamarin

Create a new Azure Mobile App

Page 26: Azure Mobile Apps with Xamarin

Quick Start in Settings and Connect a DB

Page 27: Azure Mobile Apps with Xamarin

• Azure Mobile App API - DefineTable• SQL CREATE TABLE• Sql Server Management Studio• Easy Tables• Entity Framework

For the demo just click Create ToDoItem Table:

Create your Tables

Page 28: Azure Mobile Apps with Xamarin

• Create a new Azure Mobile App backend• Build, upload, and test the server project• Create a client mobile application

or Just Use Azure’s Built-in Table API

Create a Table API using C#

Page 29: Azure Mobile Apps with Xamarin

Create a client mobile application

Click Create a new app Click Download buttonOpen Solution in Visual Studio or Xamarin Studio

Page 30: Azure Mobile Apps with Xamarin

Create a Mobile ServiceMobileService = new MobileServiceClient(

"https://myapp.azurewebsites.net");

Page 31: Azure Mobile Apps with Xamarin

Now let’s get to the data

Page 32: Azure Mobile Apps with Xamarin

Create TablesIMobileServiceSyncTable<Store> table;public async Task Init(){ const string path = "syncstore.db"; var db = new MobileServiceSQLiteStore(path); db.DefineTable<Store>();

}

var handler = new MobileServiceSyncHandler(); await MobileService.SyncContext.InitializeAsync(db, h); table = MobileService.GetSyncTable<Store>();

Page 33: Azure Mobile Apps with Xamarin

Get and Modify Datapublic async Task<IEnumerable<Store>> GetStoresAsync(){ await table.PullAsync("allStores", table.CreateQuery()); return await table.ToEnumerableAsync();}public async Task<Store> AddStoreAsync (Store store){ await table.InsertAsync (store); await table.PullAsync("allStores", table.CreateQuery()); await MobileService.SyncContext.PushAsync(); return store;}

Page 34: Azure Mobile Apps with Xamarin

Table-based SQL databaseInstances configurable using the Azure portal

Data management in:• Azure Portal• SQL Portal (Silverlight)• SQL Management Studio• REST API• Azure CLI Tools• SQL CLI

Azure SQL Database

Page 35: Azure Mobile Apps with Xamarin

• Threat detection and alerts• Auditing• Email upon anomaly detection

• Automatic tuning• Index(create and drop), query parameter, schema recommendations

• No administration required• Automatic backups and updates

Azure SQL Database

Page 36: Azure Mobile Apps with Xamarin

Data Access Anti-Pattern

Data Store

App

Data Access Calls

Data Access Calls

Data Access Calls

Courtesy of Microsoft

Page 37: Azure Mobile Apps with Xamarin

Repository Pattern

Data Store

App

RepositoryCreateRead

UpdateDelete

Data Access Calls

Data Access Calls

Data Access Calls

Courtesy of Microsoft

Page 38: Azure Mobile Apps with Xamarin

CRUD (Create, Read, Update, Delete) Create - Insert data

Read - Query data

Update - Change data

Delete - Remove data

await todoTable.InsertAsync(item);

List<TodoItem> items = await todoTable.ToListAsync(); (or ToEnumerableAsync())

await todoTable.UpdateAsync(item);

await todoTable.DeleteAsync(item);

Page 39: Azure Mobile Apps with Xamarin

public class AzureDataService{ MobileServiceClient MobileService; IMobileServiceTable<TodoItem> todoTable; public async Task NewTask(TodoItem item) { } public async Task<ObservableCollection<TodoItem>> GetTasks() { } public async Task UpdateTask(TodoItem item) { } public async Task DeleteTask(TodoItem item) { }}

CRUD Implementation Using Repository

Page 40: Azure Mobile Apps with Xamarin

private AzureDataService() { mobileService = new MobileServiceClient(Constants.ApplicationURL); this.todoTable = mobileService.GetTable<TodoItem>(); } public async Task NewTask(TodoItem item) { await todoTable.InsertAsync(item); } public async Task<ObservableCollection<TodoItem>> GetTasks() { IEnumerable<TodoItem> items = await todoTable .Where(todoItem => !todoItem.Done) .ToEnumerableAsync(); return new ObservableCollection<TodoItem>(items); } public async Task UpdateTask(TodoItem item) { await todoTable.UpdateAsync(item); } public async Task DeleteTask(TodoItem item) { await todoTable.DeleteAsync(item); }

CRUD Implementation

Page 41: Azure Mobile Apps with Xamarin

How to Sync Data?

Cloud Data ServiceMobile Device

Local data store

Cloud Data Service

Cloud data store

Courtesy of Microsoft

Page 42: Azure Mobile Apps with Xamarin
Page 43: Azure Mobile Apps with Xamarin

PushAsync PullAsync PurgeAsync

Offline Sync with Azure

Example: await mobileService.SyncContext.PushAsync();

Page 44: Azure Mobile Apps with Xamarin

public MobileServiceClient mobileService;IMobileServiceSyncTable<TodoItem> todoTable;

private AzureDataServiceSync(){ SQLitePCL.Batteries.Init(); mobileService = new MobileServiceClient("https://todotasks.azurewebsites.net"); var store = new MobileServiceSQLiteStore("localstore.db"); // SQLite setup store.DefineTable<TodoItem>(); this.mobileService.SyncContext.InitializeAsync(store); // Sync setup todoTable = MobileService.GetSyncTable();}

Setup the Service, Local Sqlite DB, and Sync

Page 45: Azure Mobile Apps with Xamarin

public async Task SyncTasks(){ await TodoTable.PullAsync("allTasks", TodoTable.CreateQuery());}

Do the Sync

Page 46: Azure Mobile Apps with Xamarin

Syncing Data with Azure

Mobile Device

SQLite

Local data store

Azure Mobile App

SQL Database

Cloud data store

PushAsync PullAsync

PurgeAsync

MobileServicePushFailedException

Courtesy of Microsoft

Page 47: Azure Mobile Apps with Xamarin

Azure Easy Tables In Azure Portal under Mobile

is a section called Easy Tables Add a table manually and

give permissions to auto-modify via API  (anonymous vs. authenticated)

Automatically updates and adds columns in the table dynamically based upon data added to the table

Page 48: Azure Mobile Apps with Xamarin

but wait, there’s more…

Page 49: Azure Mobile Apps with Xamarin

• Rolling your own account infrastructure is difficult and time-consuming

• Secure your app with prebuilt authentication providers• Facebook• Twitter• Google• Microsoft• Azure AD• Anything OAuth 2

Authentication

Page 50: Azure Mobile Apps with Xamarin

User Auth Flow (server)GOOGLE

FACEBOOK

TWITTER

MOBILE SERVICE

DEVICECREDENTIALS

(via oAuth/WebView)

MICROSOFT

IDENTITY AUTH

TOKE

N

Page 51: Azure Mobile Apps with Xamarin

• Easy-to-use, multiplatform scaled push infrastructure that allows you to send push notifications almost anywhere.

Push Notifications

Page 52: Azure Mobile Apps with Xamarin

• Sync files to Azure Storage, just like you did for structured data

File Sync

Page 53: Azure Mobile Apps with Xamarin

Azure Marketplace

Online store for Azure-configured apps and services

May have components for your app to use or include API services Azure Active Directory services Enterprise-level services

Page 54: Azure Mobile Apps with Xamarin

Azure Mobile Apps

Storage

AuthenticationPush

Courtesy of Microsoft

Page 55: Azure Mobile Apps with Xamarin

Lunch!

Dan HermesXamarin MVP, Microsoft MVPChief Executive Coder, Lexicon Systems

[email protected]

www.mobilecsharpcafe.com

@danhermes

Need an app, a direction, or a second opinion?