building a mobile enterprise application with xamarin ... · application with xamarin.forms,...

96
Building a mobile enterprise application with Xamarin.Forms, Docker, MVVM and .NET Core Gill Cleeren @gillcleeren – www.snowball.be

Upload: vandat

Post on 18-Jul-2018

252 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Building a mobile enterprise application with Xamarin.Forms,

Docker, MVVM and .NET CoreGill Cleeren

@gillcleeren – www.snowball.be

Page 2: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March
Page 3: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Kliant provides hands-on in-depth advanced software development training that is customized/tailored to your specific requirements

Our instructors are experienced developers, renowned subject matter experts and respected authors

Your contact: Eric Swanson – [email protected]

Kliant. Nice to meet you.

LA – Atlanta - London

Page 4: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Upcoming virtual courses

• Developing cross-platform apps with C# using Xamarin• 2 Day

• March 1-2

• Mastering ASP.NET Core 2 MVC• 2 Day

• March 26-27

Some seats still available!

Page 5: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Agenda

• Overall application structure

• The Xamarin application architecture• MVVM• Dependency Injection• Loose-coupled Messaging• Navigation• Service communication• Testing

• Backend architecture• .NET Core-based Microservices• Docker

Page 6: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

The Solution Structure

Page 7: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Backend in Docker hostClient apps

Xamarin.Formsmobile app

HTML web front-end

SPA

Microservice 1Product catalog

Microservice 2Ordering

Microservice 3Identity

MVC app

Page 8: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

DEMOLooking at the Solution Structure & the Finished Application

Page 9: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

The Xamarin application architecture

Page 10: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

MVVM

Page 11: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

MVVM

• Architectural pattern

• Based on data binding and commanding

• Popular for testable and maintainable XAML applications

Page 12: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

What we all did when we were young…

Write code in code-behind… Lots of it.

Data Model

View

XAML

Code-BehindEvent Handlers

Page 13: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Writing testable code however, is becoming the norm. Finally. Courtesy of MVVM.

Data Model

View

XAML

Code-Behind

View Model

State + Operations

Change notification

Data-binding and commands

Page 14: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Benefits of MVVM

• Testable

• Developers and designers can work independently

• A new XAML view can be added on top of the view models without problems

• Changes can be made in view model without risking issues with the model

Page 15: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

User interface

Window, Page or user control

Simple

No business logic

Functionalities of the View

View

MyPage.xaml

MyPage.xaml.cs

Page 16: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Sample View Code

<Entry Text="{Binding UserName.Value, Mode=TwoWay}"> </Entry>

<LabelText="{Binding UserName.Errors,Converter={StaticResource FirstValidationErrorConverter}" />

<ListViewIsVisible="{Binding Campaigns.Count, Converter={StaticResource

CountToBoolConverter}}"ItemsSource="{Binding Campaigns}"

Page 17: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Glue between view and Model

Expose state and operations

Testable

No UI elements

Functionalities of the View Model

View Model

Page 18: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Sample View Model Code

public class LoginViewModel : INotifyPropertyChanged{public string UserName{ get; set; }

public string Password{ get; set; }

public ICommand SignInCommand=> new Command(async () => await SignInAsync());

}

Page 19: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

ModelData model - services

Return data

Functionalities of the Model

Page 20: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

DEMOLooking at the Model in the application

Page 21: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Responding to changes in the (view) model

• Handled through INotifyPropertyChanged interface

• PropertyChanged raised for changes of view model or model property value changes• (View)Model property changes

• Calculated properties

• Raise event at the end of a method that makes changes to the property value

• Don’t raise PropertyChanged when the value didn’t change

• Don’t raise PropertyChanged from the constructor

• Don’t raise PropertyChanged in a loop

Page 22: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

BindableObject

public abstract class ExtendedBindableObject : BindableObject{

public void RaisePropertyChanged<T>(Expression<Func<T>> property){

var name = GetMemberInfo(property).Name;OnPropertyChanged(name);

}

private MemberInfo GetMemberInfo(Expression expression){

...}

}

Page 23: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

DEMOLooking at the ViewModels and the Views

Page 24: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Commanding

• Action is defined in one place and can be called from multiple places in the UI

• Available through ICommandinterface

• Defines Execute() and CanExecute()

• Can create our own or use built-in commands

Page 25: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

The ICommand Interface

public interface ICommand{

event EventHandler CanExecuteChanged;bool CanExecute(object parameter);void Execute(object parameter);

}

Page 26: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Exposing Operations on the View Model

public class LoginViewModel : ViewModelBase

{

public ICommand SignInCommand => new Command(async () => await SignInAsync());

public ICommand RegisterCommand => new Command(Register);

}

Page 27: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Binding Your Commands in the View

<ToolbarItem

Command="{Binding SettingsCommand}"

Text="Settings">

Page 28: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Behaviors

• Command property available only on ButtonBase-derived controls

• Other controls and interactions only possible through “behaviours”

• Use of an attached behaviour

• Use of a Xamarin.Formsbehaviour

Page 29: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

public class EventToCommandBehavior : BindableBehavior<View>{

protected override void OnAttachedTo(View visualElement){ ... }

}

EventToCommandBehavior

Page 30: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Using EventToCommandBehavior

<ListViewItemsSource="{Binding Orders}"><ListView.Behaviors>

<behaviors:EventToCommandBehaviorEventName="ItemTapped"Command="{Binding OrderDetailCommand}"EventArgsConverter="{StaticResource ItemTappedEventArgsConverter}" />

</ListView.Behaviors></ListView>

Page 31: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Using TapGestureRecognizer

<StackLayout ><Label Text="SETTINGS"/><StackLayout.GestureRecognizers><TapGestureRecognizer

Command="{Binding SettingsCommand}"NumberOfTapsRequired="1" />

</StackLayout.GestureRecognizers></StackLayout>

Page 32: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

DEMOCommanding Done Right

Page 33: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Who Knows Who?

View ModelView Model

Page 34: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

View Model-FirstView-First

Linking the View and the View Model

Page 35: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

View-First (from XAML)

<ContentPage><ContentPage.BindingContext>

<local:LoginViewModel/></ContentPage.BindingContext>

</ContentPage>

Page 36: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

View First (from code)

public LoginView(){

InitializeComponent();BindingContext = new LoginViewModel();

}

Page 37: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

The View Model Locator

ViewModelLocator

View Model 1

View Model 2

View Model n

View 1

View 2

View n

Page 38: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

DEMOThe ViewModel Locator

Page 39: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Dependency Injection

Page 40: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Dependency Injection

• Type of inversion of control (IoC)

• Another class is responsible for obtaining the required dependency

• Results in more loose coupling

• Container handles instantiation as well as lifetime of objects

• Autofac is commonly used

• Many others exist

Page 41: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Dependency Injection

IContainerProfileViewModel OrderService

IOrderService

Page 42: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Dependency Injection

public class ProfileViewModel : ViewModelBase{

private IOrderService _orderService;

public ProfileViewModel(IOrderService orderService){_orderService = orderService;

}}

Page 43: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Advantages of using Dependency Injection• Better testability through easy

way of using mock versions

• Higher maintainability by making it easy to add new classes• Central location

• Dependencies don’t affect the class that uses them

• Classes don’t need to manage their dependencies• It’s handled for them

• They add complexity• Not always a good idea for

small apps…

Page 44: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Container registration

• Happens at application startup

builder.RegisterType<NavigationService>().As<INavigationService>().SingleInstance();builder.RegisterType<DialogService>().As<IDialogService>();

Page 45: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Resolving dependencies

• If the requested type can’t be found, an exception is thrown

• If a type is requested that registered as singleton, we always will get back the same instance• A new instance will only be created once, the first time it’s requested

• Otherwise, a new instance will always be returned

NavigationService = ViewModelLocator.Resolve<INavigationService>();

Page 46: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

DEMOWorking with Dependency Injection

Page 47: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Loose-coupled Messaging

Page 48: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

View Model communication

View Model

View Model

View Model View Model

View Model

View Model

View Model View Model

Page 49: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Messaging Center built-in in Xamarin.Forms

Data Model

View

XAML

Code-Behind

Data Model

View

XAML

Code-Behind

Message

View Model

State + OperationsView Model

State + Operations

View

XAML

Code-Behind

MessageMessaging Center

(XF)

View Model

State + Operations

Publish messages

Subscribe to messages

Page 50: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Messaging Center

• Implements pub-sub model for us already

• Built-in in Xamarin.Forms

• Multicast supported

• Based on strings (not always perfect)

Page 51: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

DEMOWorking with Messages and the Messaging Center

Page 52: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Navigation

Page 53: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Navigation and MVVM

• Navigation isn’t always easy to include in an MVVM scenario• No tight-coupling can be

introduced• Who is responsible for

navigation? View Model? View?

• How can we pass parameters during navigation?

• Xamarin.Forms comes with INavigation interface• Will be wrapped as it’s too

basic for real-life scenarios

Page 54: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Our Own NavigationService

• Must be registered in the Dependency Injection system

public interface INavigationService{ViewModelBase PreviousPageViewModel { get; }

Task InitializeAsync();

Task NavigateToAsync<TViewModel>() where TViewModel : ViewModelBase;

Task NavigateToAsync<TViewModel>(object parameter) where TViewModel : ViewModelBase;

Task RemoveLastFromBackStackAsync();

Task RemoveBackStackAsync();}

Page 55: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

DEMOAdding Navigation

Page 56: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Service communication

Page 57: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Take some REST

• REST: Representational State Transfer

• Based on open HTTP standards

• Open for all types of applications

• Works with Resources

• We’ll send requests to access these resources

• URI and HTTP method are used for this

• Results in HTTP Status code

• 200, 404… based on result of request

Page 58: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Communicating with a REST API

• Apps will typically use services for making the data request• Are responsible for communication with the actual API

• Controllers on API microservices return DTOs

• Are transferred to the application

• App can use HttpClient class• Works with JSON

• Returns HttpResponseMessage after receiving a request

• Can then be read and parsed • Json.NET

Page 59: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Loading data from the service

public override async Task InitializeAsync(object navigationData){IsBusy = true;Products = await _productsService.GetCatalogAsync();Brands = await _productsService.GetCatalogBrandAsync();Types = await _productsService.GetCatalogTypeAsync();IsBusy = false;

}

Page 60: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Using the Data Servicepublic async Task<ObservableCollection<CatalogItem>> GetCatalogAsync(){UriBuilder builder = new UriBuilder(GlobalSetting.Instance.CatalogEndpoint);

builder.Path = "api/v1/catalog/items";string uri = builder.ToString();

CatalogRoot catalog = await _requestProvider.GetAsync<CatalogRoot>(uri);

if (catalog?.Data != null){ServicesHelper.FixCatalogItemPictureUri(catalog?.Data);

return catalog?.Data.ToObservableCollection();}elsereturn new ObservableCollection<CatalogItem>();

}

Page 61: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Using the RequestProvider

public async Task<TResult> GetAsync<TResult>(string uri, string token = ""){HttpClient httpClient = CreateHttpClient(token);HttpResponseMessage response = await httpClient.GetAsync(uri);

await HandleResponse(response);string serialized = await response.Content.ReadAsStringAsync();

TResult result = await Task.Run(() => JsonConvert.DeserializeObject<TResult>(serialized, _serializerSettings));

return result;}

Page 62: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Action in the Controller in the Microservice[HttpGet][Route("[action]")]public async Task<IActionResult> Items([FromQuery]int pageSize = 10, [FromQuery]intpageIndex = 0){var totalItems = await _catalogContext.CatalogItems.LongCountAsync();

var itemsOnPage = await _catalogContext.CatalogItems.OrderBy(c => c.Name).Skip(pageSize * pageIndex).Take(pageSize).ToListAsync();

itemsOnPage = ChangeUriPlaceholder(itemsOnPage);

var model = new PaginatedItemsViewModel<CatalogItem>(pageIndex, pageSize, totalItems, itemsOnPage);

return Ok(model);}

Page 63: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

DEMOAccessing Remote Data

Page 64: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Unit Testing

Page 65: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Testing is REALLY needed

• Different devices

• Network

• Performance

• Different resolutions

• Different OS versions

Page 66: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

A unit test is an automated piece of code that invokes a unit of work in the system and then checks a single assumption about the behaviorof that unit of work.From The Art of Unit Testing, Roy Osherove

Page 67: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Unit testing

• Block of code that needs to be tested

• Public API

• Isolated

• Fast

• Refactor with confidence

• Automated

• Independent• Dependency injection will help here

IOrderService

OrderServiceMockOrderService

Page 68: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Why we need unit tests?

• Find bugs

• No fear to break something when making changes

• Improve code quality

• Code documentation

Page 69: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Structure of a unit test

• Arrange

• Act

• Assert

Page 70: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

DEMOUnit Testing

Page 71: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Backend architecture

Page 72: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

ASP.NET Core

Page 73: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

.NET Core

“.NET Core is a general purpose development platform maintained by Microsoft and the .NET community on GitHub. It is cross-platform, supporting Windows, macOS and Linux, and can be used in device,

cloud, and embedded/IoT scenarios.”

source: https://docs.microsoft.com/en-us/dotnet/articles/core

Page 74: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

ASP.NET Core

“ASP.NET Core is a new open-source and cross-platform framework for building modern cloud based internet connected applications, such as

web apps, IoT apps and mobile backends.”

source: https://docs.microsoft.com/en-us/aspnet/core

Page 75: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

ASP.NET Core

• Built on top of .NET Core

• Lightweight

• Cross-platform• Windows, Mac & Linux

Easy in combination with Docker and Microservices

Page 76: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

What does ASP.NET Core bring us then?

• Unification between MVC and Web API

• Dependency Injection

• Modular HTTP request pipeline

• Based on NuGet

• Cloud-ready

• IIS or self-host

• Open source

• Community focus

• Cross-platform

• New tooling

• Better integration of client-side frameworks

• Command-line support

Page 77: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Containerized Microservices

Page 78: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Monoliths

• Client-server often results in tiered applications

• Specific technology used per tier

• Known as monolithic applications

• Often have tight coupling between components in each tier

• Components can’t be scaled easily

• Testing individual components might also be hard

Page 79: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Monoliths

• Not being able to scale can be issue for cloud readiness

• All layers typically are required

• Scaling is cloning the entire application onto multiple machines

Page 80: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Monolithic applications

Page 81: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Enter microservices

• Microservices are easier for deployment and development• Better agility • Better combination with cloud

• App will be decomposed into several components• Components together deliver app functionality

• Microservice = small app, independent concern• Have contracts to communicate with other services

• Typical microservices• Shopping cart• Payment system• Inventory system

Page 82: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Enter microservices

• Can scale out independently • If an area requires more processing power, can be scaled out separately

• Other parts can remain the same

• Scale-out can be instantaneous• Web front-end for handling more

incoming traffic

Page 83: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Enter microservices

• Microservices manage their own data• Locally on the server on which they run

• Avoid network overhead

• Faster for processing

• Even eliminate need for caching

• Support for independent updates• Faster evolution

• Rolling updates, onto subset of instances of single service support rollback

Page 84: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Benefits of using microservices• Small

• Evolve easily

• Scale-out independently

• Isolate issues to the faulty microservice

• Can use latest and greatest

• Not constrained to using older technologies

Page 85: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Disadvantages of microservices• Partitioning a real application is hard

• Complex

• Intercommunication between services

• Eventual consistency

• Atomic transactions often not supported

• Deployment (initial) might be harder

• Direct client-to-microservice communication might not be a good idea

Page 86: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

DEMOLooking at the Microservices

Page 87: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

“Containerization is an approach to software development in which an application and its versioned set of dependencies, plus its environment configuration abstracted as deployment manifest files, are packaged together as a container image, tested as a unit, and deployed to a host operating system”

Page 88: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Adding containers to the mix• Container is isolated, resource

controlled and portable operating environment

• Applications in containers run without touching resources of host or other containers

• Acts like a VM or physical machine

• Work great in combination with microservices

• Docker is most commonly used approach here

Page 89: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

What’s a container really?

• Container runs an operating system

• Contains file system

• Can be accessed over the network like a real machine/VM

• Contain the application (and dependencies)

• In general, require less resources to run than regular VMs

• Allow for easy and fast scale-up by adding new containers

Page 90: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Adding containers to the mix

Page 91: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Using Docker to host the microservices

• Each container hosts a separate part of the application• Single area of functionality

• Each microservice has its own database• Allows for full decoupling

• Consistency is eventual

• Can be improved using application events• Service bus

Microservice 1Product catalog

Microservice 2Ordering

Microservice 3Identity

Page 92: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

DEMOContainerization

Page 93: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Summary

• MVVM is the go-to standard for building enterprise-level Xamarin.Forms apps

• .NET Core is a good choice for building microservices

• Docker helps with deployment of microservices

Page 94: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Thanks!

Page 95: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March
Page 96: Building a mobile enterprise application with Xamarin ... · application with Xamarin.Forms, Docker, ... •Developing cross-platform apps with C# using Xamarin •2 Day •March

Building a mobile enterprise application with Xamarin.Forms,

Docker, MVVM and .NET CoreGill Cleeren

@gillcleeren – www.snowball.be