cross platform development in c# (dddnorth 2013)

39
Cross Platform Mobile Development in C# Ross Dargan @rossdargan

Upload: rossdargan

Post on 20-Jun-2015

2.025 views

Category:

Technology


0 download

DESCRIPTION

Shows how to separate code to allow for maximum code reuse across iOS, Android, Windows Phone, Windows Store, WPF, and silverlight applications. Code can be found here: https://github.com/rossdargan/Talks

TRANSCRIPT

Page 1: Cross Platform Development in C# (DDDNorth 2013)

Cross Platform Mobile Development in C#

Ross Dargan

@rossdargan

Page 2: Cross Platform Development in C# (DDDNorth 2013)

What I’m going to cover

Target 95% of the UK mobile market in C#

With maximum code re-use

Page 3: Cross Platform Development in C# (DDDNorth 2013)

What I’m going to cover

Page 4: Cross Platform Development in C# (DDDNorth 2013)

What I won’t cover

The other cross platform options (http://propertycross.com/)

A full walkthrough of the tooling

Blackberry

Page 5: Cross Platform Development in C# (DDDNorth 2013)

Android56%

Windows12%

iOS27%

Blackberry4%

Other1%

UK Smartphone Landscape

Source: http://bit.ly/19d2thf

Page 6: Cross Platform Development in C# (DDDNorth 2013)

Native DevelopmentiOS

Objective C

Xcode

Windows Phone

C#/Visual Basic/F#

Visual Studio

Android

Java

Eclipse

IntelliJ

Page 7: Cross Platform Development in C# (DDDNorth 2013)

Xamarin

• C# and .NET for Android, iOS and Mac• Full native APIs

Page 8: Cross Platform Development in C# (DDDNorth 2013)

The bad…

• Price $2000 per dev per year• Still need a mac• Write once, run (and test!) everywhere• Tooling is still a bit immature (but the

support is great!)• Need to write each UI separately

Page 9: Cross Platform Development in C# (DDDNorth 2013)

UI Considerations

Page 10: Cross Platform Development in C# (DDDNorth 2013)

DEMOHello World

Page 11: Cross Platform Development in C# (DDDNorth 2013)

So… Sharing

Page 12: Cross Platform Development in C# (DDDNorth 2013)

SHARING CODEFile Linking

Page 13: Cross Platform Development in C# (DDDNorth 2013)

File Linking

Page 14: Cross Platform Development in C# (DDDNorth 2013)

DEMOFile Linking

Page 15: Cross Platform Development in C# (DDDNorth 2013)

File Linking

Pros

• Quick

Cons

• Messy code• Progressively more

difficult to maintain as you add implementations

• Very hard to test• File linking is a PITA to

remember as you add files

Page 16: Cross Platform Development in C# (DDDNorth 2013)

SHARING CODEPortable Class Libraries

Page 17: Cross Platform Development in C# (DDDNorth 2013)

Adding a PCL

Page 18: Cross Platform Development in C# (DDDNorth 2013)

Great… but

Still need platform specific code somehow…

Page 19: Cross Platform Development in C# (DDDNorth 2013)

SHARING CODEAbstract Classes

Page 20: Cross Platform Development in C# (DDDNorth 2013)

Multiple Concrete Classes

Page 21: Cross Platform Development in C# (DDDNorth 2013)

DEMOAbstract Classes

Page 22: Cross Platform Development in C# (DDDNorth 2013)

Abstract Classes

Pros

• Quick• Easier to maintain• Follows Open/Closed

principal

Cons

• Difficult to test• Pretty inflexible

Page 23: Cross Platform Development in C# (DDDNorth 2013)

SHARING CODEDependency Injection

Page 24: Cross Platform Development in C# (DDDNorth 2013)

Dependency Injection

• Helps separate code (Single responsibility)• Allows you to unit test’s discrete parts of

the implementation – no God classes• You can inject in platform specific

implementations• Inversion of Control allows you to swap

implementations easily

Page 25: Cross Platform Development in C# (DDDNorth 2013)

DEMOPCL & DI demo

Page 26: Cross Platform Development in C# (DDDNorth 2013)

Dependency Injection

Pros

• Very loosely coupled code (reuse)

• Unit tests are easy• Single Responsibility• Open/Closed

Cons

• • •

Page 27: Cross Platform Development in C# (DDDNorth 2013)
Page 28: Cross Platform Development in C# (DDDNorth 2013)
Page 29: Cross Platform Development in C# (DDDNorth 2013)

What are the downsides to using Dependency Injection?

It can be dangerous for your career because it may increase your overall knowledge of good API design. Once you learn how proper loosely coupled code can look like, it may turn out that you will have to decline lots of job offers because you would otherwise have to work with tightly coupled legacy apps. Happens to me a lot :)

Source: http://bit.ly/com6aD

Page 30: Cross Platform Development in C# (DDDNorth 2013)

Dependency Injection

Pros

• Very loosely coupled code (reuse)

• Unit tests are easy• Single Responsibility• Open/Closed

Cons

• More code• Bloated constructors

Page 31: Cross Platform Development in C# (DDDNorth 2013)
Page 32: Cross Platform Development in C# (DDDNorth 2013)

Can we squeeze any more reuse out?

Page 33: Cross Platform Development in C# (DDDNorth 2013)

MAXIMIZE UI CODE REUSEMVVM

Page 34: Cross Platform Development in C# (DDDNorth 2013)

MVVM

Platform Specific Code

Shared Code

Page 35: Cross Platform Development in C# (DDDNorth 2013)

Demo

Page 36: Cross Platform Development in C# (DDDNorth 2013)

Improving Code Reuse in 3 easy ways

• Heavy use of dependency injection– Allows different implementations for different

platforms

• Use PCLs (and file linking for the time being)

• Use View Models to reuse presentation code

Page 37: Cross Platform Development in C# (DDDNorth 2013)

3 layers of win

MVVMCross

Xamarin

Visual Studio

Page 38: Cross Platform Development in C# (DDDNorth 2013)

Links

• http://xamarin.com/• http://mvvmcross.wordpress.com/• http://propertycross.com/• https://github.com/rossdargan/Talks• Dependency Injection in .Net by Mark

Seemann (ISBN: 1-935182-50-1)• http://www.icndb.com/api/

Page 39: Cross Platform Development in C# (DDDNorth 2013)

Questions