Architectures and techniques for a portable app

29
PTXug - Xamarin Talks #2 Lisbon, June 27 th 2015 Architecture and techniques for a portable app

Transcript of Architectures and techniques for a portable app

PTXug - Xamarin Talks #2Lisbon, June 27th 2015

Architecture and techniques for a

portable app

Speaker

• Indie App developer (ImaginationOverflow)

• Freelancer

• Masters Student in Instituto Superior de Engenharia de Lisboa (ISEL)

C:\> ping me @dvd_pt http://blog.imaginationoverflow.com

Architecture

Project Management

Programming Techniques

ImaginationOverflow Apps

Agenda

Architecture

Model View Controller

(MVC)

Model View ViewModel

(MVVM)

MVC• Views call controller operations

• Controllers update the model or call its operations

• Controller notifies view• Android – callbacks, async tasks, etc• iOS – callbacks, delegates, etc

MVC

class View : Activity{

private SomeController _controller = ...;

public void OnButtonClicked(...){

_controller.BeginAction(()=>{

someLabel.Text = ...;});

}}

class SomeController {

private SomeModel _model = ...;

public async void BeginAction(Action callback){

await _model.DoStuff();callback();

}

}

MVVM• Views binds to ViewModels

• Views invoke ViewModel commands

• ViewModels update the model or invoke its operations

• ViewModels update its properties

• Views reacts to the change in binded properties and update itself

DemoMVVM in Action

Let’s code

MVC vs MVVMMVC• Controllers know their views• Ctor passage new Controller(this)• Direct parametrization controller.DoStuff(this)• Callbacks

• Is not data oriented

• Its goal is to separate concerns

MVC vs MVVM

MVVM• Data binding

• ViewModels don’t know the View

• ViewModels work as a View Contract• Abstracting the UI Framework

• Async by design

Project Management

• Shared projects

• Portable Class Libraries (PCL)

Shared Projects

Shared Projects

• Compiles inside the application project• All platform API’s are available

• Good to implement app specific functionality/contracts• Navigation• Register dependencies on DI

• Limited usage• Doesn’t generate any deliverable

• #IF madness

Portable Class Libraries (PCL)

Windows Phone

Android

iOS

Windows

Portable Class Libraries (PCL)

• “Platform independent”

• Subset of target platforms framework

• Reusable

• Available APIs can be very limited

• Almost mandatory:• Interface based programming• Dependency Injection

Programming Techniques

• Interface based programming

• Inversion of control (IOC)

• Dependency Injection (DI)

• Localisation

Interface based programming

• A contract of related operations

• Contracts can be reused

• Modular

• Loosely coupled

• Microservices

Interface based programming

Windows

iOS

Android

IToastNotificationService

PCL

PCL

PCL

SomeViewModel

SomeModel

uses

Inversion of Control

• Clearly stating the component dependencies• Via ctor, properties, etc

• Dependencies lifecycle is no longer an issue

• Using interface based programming is a plus

• Focus only on the behavior

Inversion of Controlpublic class SomeViewModel{

private IToastNotificationService _toastNotficationServicepublic SomeViewModel(IToastNotificationService toastNotficationService){

_toastNotficationService = toastNotficationService;}

private ICommand _showToastCommandpublic ICommand ShowToastCommand{

get{

return _showToastCommand??_showToastCommand =new RelayCommand(()=>{

_toastNotficationService.ShowToast("Toasttext");})

}}

}

public interface IToastNotificationService{

string ShowToast(string toast);}

Dependency Injection

• Container of initializers

• Inject to target components its dependencies.

• Manage injected objects lifecycle• Creation • Disposal

DemoProgramming techniques on

Android and Windows

Let’s code

LocalisationResx Files• Centralized string resource manager

• Static access AppResources.SomeString

• Can be on PCLs

• Fully supported by Xamarin

• Not compatible with Android and iOS string resources• Use t4 templates

ImaginationOverflow Apps

ImaginationOverflow Apps

Wrap Up• MVVM is the pattern for Portable apps

• Shared projects for app related

• PCLs for generic

• Interface based programming, IOC and DI always

• Make your code as much portable as it can be

• Be aware of the Return of Investment (ROI)

Q & A

Sponsors

THANK YOU !C:\> ping me @dvd_pt