Slide 1

Slide 1 text

MvvmCross Introduction 13th December 2012 @slodge

Slide 2

Slide 2 text

Presentation Model Represent the state and behavior of the presentation independently of the GUI controls used in the interface. http://martinfowler.com/eaaDev/PresentationModel.html

Slide 3

Slide 3 text

In 2005… Model/View/ViewModel is a variation of Model/View/Controller that is tailored for modern UI development platforms where the View is the responsibility of a designer rather than a classic developer. Tales from the Smart Client, John Grossman http://blogs.msdn.com/b/johngossman/archive/2005/10/08/478683.aspx

Slide 4

Slide 4 text

Also in 2005… • 10 years of dev – C++, C, VB, Java, JavaScript, LISP, SmallTalk, Delphi, … • A year off travelling – from Argentina to Zambia • DeveloperDeveloperDeveloper • 1 conclusion:

Slide 5

Slide 5 text

MvvmCross Introduction 13th December 2012 @slodge Evolving the dinosaur

Slide 6

Slide 6 text

What we’ll cover… • MVVM Theory • .Net MVVM • A practical example – some code! • Interface Driven Development – Portable Class Libraries – Unit Testing – Plugins • Some examples

Slide 7

Slide 7 text

What we’ll cover… • MVVM Theory • .Net MVVM • A practical example – some code! • Interface Driven Development – Portable Class Libraries – Unit Testing – Plugins • Some examples

Slide 8

Slide 8 text

M-V-VM

Slide 9

Slide 9 text

Detailed flow

Slide 10

Slide 10 text

What we’ll cover… • MVVM Theory • .Net MVVM • A practical example – some code! • Interface Driven Development – Portable Class Libraries – Unit Testing – Plugins • Some examples

Slide 11

Slide 11 text

ViewModels Public Properties private bool _isSearching; public bool IsSearching { get { return _isSearching; } set { _isSearching = value; RaisePropertyChanged("IsSearching"); } }

Slide 12

Slide 12 text

For ViewModel Changes public interface INotifyPropertyChanged { event PropertyChangedEventHandler PropertyChanged; } public class PropertyChangedEventArgs : EventArgs { public string PropertyName { get; } }

Slide 13

Slide 13 text

For Collections public interface INotifyCollectionChanged { event NotifyCollectionChangedEventHandler CollectionChanged; } public enum NotifyCollectionChangedAction { Add, Remove, Replace, Move, Reset, } public class NotifyCollectionChangedEventArgs : EventArg { public NotifyCollectionChangedAction Action { get; } public IList NewItems { get; } public IList OldItems { get; } public int NewStartingIndex { get; } public int OldStartingIndex { get; } }

Slide 14

Slide 14 text

For Actions public interface ICommand { event EventHandler CanExecuteChanged; bool CanExecute(object parameter); void Execute(object parameter); }

Slide 15

Slide 15 text

.Net Implementation ICommand Public Property Set INotifyPropertyChanged INotifyCollectionChanged Public Property Get

Slide 16

Slide 16 text

Why? To Enable • Awesome UI and Data Development • Unit Testing of code • Large applications to have a common architecture • Different platforms can share code

Slide 17

Slide 17 text

What we’ll cover… • MVVM Theory • .Net MVVM • A practical example – some code! • Interface Driven Development – Portable Class Libraries – Unit Testing – Plugins • Some examples

Slide 18

Slide 18 text

What is MvvmCross? Code!

Slide 19

Slide 19 text

Code evolution I • Single Mono for Android Project • Good separation of UI from ‘Model’ code • Simple – it works • But: – No testing – No testability – Portability by cut/paste

Slide 20

Slide 20 text

Code Evolution 2 • MvvmCross Library switched in – PCL code – Formal DI/IoC used • On UI: – DataBinding arrived – Code got much thinner! – XML got bigger • Not all win: – External Dependencies got larger – Code overall increased in size

Slide 21

Slide 21 text

Code Evolution 3 • Cross Platform • All UIs MVVM • 100% shared application logic • 100% shared test harness

Slide 22

Slide 22 text

Data-Binding WP/WinRT 99% Xaml Droid Mainly Axml Some .Dialog Touch Some .Dialog Some .XIB Some C#

Slide 23

Slide 23 text

What we’ll cover… • MVVM Theory • .Net MVVM • A practical example – some code! • Interface Driven Development – Portable Class Libraries – Unit Testing – Plugins • Some examples

Slide 24

Slide 24 text

Portable Class Libraries

Slide 25

Slide 25 text

Unit Testing “I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence. … When coding on a team, I modify my strategy to carefully test code that we, collectively, tend to get wrong.” Kent Beck http://stackoverflow.com/questions/153234/how-deep-are-your-unit-tests

Slide 26

Slide 26 text

Plugin – Native abstractions public interface IMvxComposeEmailTask { void ComposeEmail(string to, string cc, string subject, string body, bool isHtml); } protected void ComposeEmail(string to, string subject, string body) { Cirrious.MvvmCross.Plugins.Email.PluginLoader.Instance.EnsureLoaded(); var task = this.GetService(); task.ComposeEmail(to, null, subject, body, false); } public class MvxComposeEmailTask : MvxWindowsPhoneTask, IMvxComposeEmailTask { public void ComposeEmail(string to, string cc, string subject, string body, bool isHtml) { var task = new EmailComposeTask() { To = to, Subject = subject, Cc = cc, Body = body }; DoWithInvalidOperationProtection(task.Show); } } 1. Declare common functionality (an interface) 2. Write platform specific implementations 3. In apps, use the interface and not the implementation

Slide 27

Slide 27 text

Sphero – Plugin Magic • Plugin Magic • Each Plugin: – 1 PCL – 1 Assembly per platform

Slide 28

Slide 28 text

Why? To Enable • Awesome UI and Data Development • Unit Testing of code • Large applications to have a common architecture • Different platforms can share code

Slide 29

Slide 29 text

What we’ll cover… • MVVM Theory • .Net MVVM • A practical example – some code! • Interface Driven Development – Portable Class Libraries – Unit Testing – Plugins • Some examples

Slide 30

Slide 30 text

MonoCross

Slide 31

Slide 31 text

A team in a galaxy far far away @imaji @mrlacey @sichy @slodge @touch4apps …

Slide 32

Slide 32 text

Redth in Canada https://github.com/Redth/WshLst/

Slide 33

Slide 33 text

Rune in Norway https://github.com/runegri/CrossBox

Slide 34

Slide 34 text

Jason in UK http://www.aviva.co.uk/drive/

Slide 35

Slide 35 text

CheeseBaron in Denmark http://blog.ostebaronen.dk/

Slide 36

Slide 36 text

Greg in NYC http://bit.ly/mvxgshac

Slide 37

Slide 37 text

JSON.Net Downunder (?)

Slide 38

Slide 38 text

Olivier in France http://www.e-naxos.com/UsIndex.html

Slide 39

Slide 39 text

Dan in Italy http://bit.ly/mvxDanA

Slide 40

Slide 40 text

Zoldeper in Hungary? https://github.com/Zoldeper/Blooor

Slide 41

Slide 41 text

Daniel in Redmond http://channel9.msdn.com/Events/Build/2012/3-004

Slide 42

Slide 42 text

What we’ve covered… • MVVM Theory • .Net MVVM • A practical example – some code! • Interface Driven Development – Portable Class Libraries – Unit Testing – Plugins • Some examples

Slide 43

Slide 43 text

To join in… • Tool up • Share • Reuse • Test • Architect If you want to join in:

Slide 44

Slide 44 text

MS-PL on GitHub http://github.com/slodge/MvvmCross

Slide 45

Slide 45 text

Some other talks available http://bit.ly/mvxTweetPic

Slide 46

Slide 46 text

C# - 1 Stack - Cloud to Mobile WP7 iOS Droid Win8 Data Access Business Logic Presentation Service Consumption Business Logic Local Data/Services UI Logic

Slide 47

Slide 47 text

Not as cool as dinosaurs WP7 iOS Droid Win8 Data Access Business Logic Presentation Service Consumption Business Logic Local Data/Services UI Logic

Slide 48

Slide 48 text

Some credits Images from Wikipedia Commons: • http://en.wikipedia.org/wiki/File:Macronaria_scrubbed_enh.jpg • http://en.wikipedia.org/wiki/File:Human- eoraptor_size_comparison%28v2%29.png Diagrams from Java – ZK • http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/MVVM Sample projects as credited inline

Slide 49

Slide 49 text

Thanks for listening… http://cirrious.com http://slodge.blogspot.com http://github.com/slodge/mvvmcross me@slodge.com @slodge Stuart Lodge, I’m a Dinosaur

Slide 50

Slide 50 text

Xamarin Seminar 13th December 2012 Please give us your feedback Follow us on Twitter http://bit.ly/xamfeedback @XamarinHQ