Upload
danhermes
View
237
Download
3
Embed Size (px)
Dan HermesXamarin MVPPresident, Lexicon Systems@danhermes
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
“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
Book Tour
Book Tour
Why are we here?
We Apps
Everybody Apps!
189Mdownloads
a day
200mins on phone
127mins in
apps
The average app user has 36 apps installed on his or her phone.
Only 1/4 are used daily:
1/4 of apps are never used!
Why not?
Bad App Experiences
• Slow or laggy• Crashes• Unintuitive & bad UX• Features not as advertised• Data not available when you need it
Your app craves connection more than
you do.
http://opensignal.com/coverage-maps
No app is an island
Every app needs a server…DatabasesServices
AuthenticationNotifications
Files
…in the cloud, of course
Plenty of Options
Amazon Web Services
Azure Mobile Apps IBM MobileFirst
Oracle Mobile Cloud
SQLCIipher
Couchbase Realm
SQLite-net
• 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?
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
What are Azure Mobile Apps?
Storage
AuthenticationPush
Courtesy of Microsoft
Storage, Authentication, and Push Notifications
Courtesy of Microsoft
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
Let’s add a backend
Create a new Azure Mobile App
Quick Start in Settings and Connect a DB
• 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
• 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#
Create a client mobile application
Click Create a new app Click Download buttonOpen Solution in Visual Studio or Xamarin Studio
Create a Mobile ServiceMobileService = new MobileServiceClient(
"https://myapp.azurewebsites.net");
Now let’s get to the data
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>();
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;}
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
• 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
Data Access Anti-Pattern
Data Store
App
Data Access Calls
Data Access Calls
Data Access Calls
Courtesy of Microsoft
Repository Pattern
Data Store
App
RepositoryCreateRead
UpdateDelete
Data Access Calls
Data Access Calls
Data Access Calls
Courtesy of Microsoft
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);
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
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
How to Sync Data?
Cloud Data ServiceMobile Device
Local data store
Cloud Data Service
Cloud data store
Courtesy of Microsoft
PushAsync PullAsync PurgeAsync
Offline Sync with Azure
Example: await mobileService.SyncContext.PushAsync();
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
public async Task SyncTasks(){ await TodoTable.PullAsync("allTasks", TodoTable.CreateQuery());}
Do the Sync
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
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
but wait, there’s more…
• 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
User Auth Flow (server)GOOGLE
MOBILE SERVICE
DEVICECREDENTIALS
(via oAuth/WebView)
MICROSOFT
IDENTITY AUTH
TOKE
N
• Easy-to-use, multiplatform scaled push infrastructure that allows you to send push notifications almost anywhere.
Push Notifications
• Sync files to Azure Storage, just like you did for structured data
File Sync
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
Azure Mobile Apps
Storage
AuthenticationPush
Courtesy of Microsoft
Lunch!
Dan HermesXamarin MVP, Microsoft MVPChief Executive Coder, Lexicon Systems
www.mobilecsharpcafe.com
@danhermes
Need an app, a direction, or a second opinion?