Page 42 - MSDN Magazine, June 2017
P. 42

Figure 8 Authentication.cs in Android
inheritance. In the PCL project, add a code file called Constants.cs, whose content is the following:
public static class Constants {
public const string BaseUrl = "https://mobile-{your-app-id}.azurewebsites.net/"; }
With this approach, you don’t need to repeat the same common properties for each class; you can simply take advantage of inheritance.
This constant will contain the URL of your back-end service and will be used instead of writing the URL manually every time.
Implementing Authentication
The Azure Mobile Client SDK makes authenticating a user very simple. In fact, you just need to invoke the MobileService-
Figure 10 Implementing a Data Access Layer
using System;
using MyBookshelf.Authentication;
using Microsoft.WindowsAzure.MobileServices; using Xamarin.Forms;
using System.Threading.Tasks;
using MyBookshelf.Droid;
[assembly: Dependency(typeof(Authentication))] namespace MyBookshelf.Droid
{
public class Authentication : IAuthentication {
public async Task<MobileServiceUser> LoginAsync(MobileServiceClient client, MobileServiceAuthenticationProvider provider)
{ try
{
var user = await client.LoginAsync(Forms.Context, provider); return user;
}
catch (Exception) {
return null; }
} }
}
you now add to the PCL project a folder called Model and two code files called TableBase.cs and Book.cs. Figure 7 shows the full code for both class definitions.
With this approach, you don’t need to repeat the same common properties for each class; you can simply take advantage of
Figure 9 Authentication.cs in iOS
using Microsoft.WindowsAzure.MobileServices; using MyBookshelf.Model;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace MyBookshelf.DataAccess {
public class DataManager {
public MobileServiceClient Client { get; set; }
public DataManager(string serviceUrl) {
Client = new MobileServiceClient(serviceUrl); }
public async Task SaveAsync<T>(IEnumerable<T> items) where T : TableBase {
var table = this.Client.GetTable<T>();
foreach (T item in items) {
if (item.Id == null) {
item.Id = Guid.NewGuid().ToString("N");
await table.InsertAsync(item); }
else {
await table.UpdateAsync(item); }
} }
public async Task<IEnumerable<T>> LoadAsync<T>(string userId) where T : TableBase
{
var table = this.Client.GetTable<T>();
var query = await table.Where(t => t.UserId == userId).ToEnumerableAsync();
return query; }
} }
using Microsoft.WindowsAzure.MobileServices; using MyBookshelf.Authentication;
using MyBookshelf.iOS;
using System;
using System.Threading.Tasks;
[assembly: Xamarin.Forms.Dependency(typeof(Authentication))] namespace MyBookshelf.iOS
{
public class Authentication : IAuthentication {
public async Task<MobileServiceUser> LoginAsync(MobileServiceClient client, MobileServiceAuthenticationProvider provider)
{ try
{
// Attempt to find root view controller to present authentication page var window = UIKit.UIApplication.SharedApplication.KeyWindow;
var root = window.RootViewController;
if (root != null)
{
var current = root;
while (current.PresentedViewController != null) {
current = current.PresentedViewController; }
// Login and save user status
var user = await client.LoginAsync(current, provider); return user;
}
return null; }
catch (Exception) {
return null; }
} }
}
38 msdn magazine
Xamarin



















   40   41   42   43   44