Patterns for Cross Platform Mobile Development - XAM250

Sometimes you need to access platform-specific APIs that are not intrinsically available from your shared code. For example, you might want to take a picture, perform file I/O, or play audio. The recommended approach is to code an abstraction layer that will let you invoke the platform-specific API from shared code. This course shows you three techniques for doing this: Factory pattern, Service Locator pattern, and Dependency Injection.

Upon completion you will be able to:

Course exercise download

Exercise materials are available from GitHub. The code can be cloned through a desktop Git client or downloaded directly as a ZIP file.

If you are using Windows, try to select a location with a shorter path length to make sure you don't exceed the 260 character limit.

Lecture

Here are the lecture slides.

Exercises

  1. Exercise 1: Use the Factory Pattern to access a dependency from shared code (XAM250)

    This exercise will take an existing iOS, Android, or Windows project and pull out sharable code into a Portable Class Library (PCL), using the Factory Pattern to isolate the platform-specific code to read and write the storage file used to display quotes.

  2. Exercise 2: Add an abstraction and Service Locator into our project to find and use a platform-specific feature. (XAM250)

    This exercise will define a service for performing text-to-speech and then create a Service Locator to find the implementation of that service which is platform-specific. This is intended to be done as part of the online class, if you are working through this on your own you can follow the step-by-step instructions shown below.

  3. Exercise 3: Switch to a DI/IoC container and inject our dependency into our code. (XAM250)

    This final exercise will replace the Factory pattern used in the first exercise with a Dependency Injection (DI) container that will supply the quote loader as part of the constructor. You'll then optionally, remove the Service Locator as well and just use the DI/IoC container injection approach.

Additional resources

Additional Documentation

Exploring the Factory Pattern (MSDN) Common Service Locator (Microsoft P&P) Inversion of Control Containers and the Dependency Injection pattern (Martin Fowler) Dependency Injection (MSDN Magazine) IoC Containers with Xamarin