7 Key Decisions for Any New Xamarin Project

While there is no shortage of information out there to help you get started with Xamarin, making the most of the right tools and established best practices can really kickstart your development efforts.

On any new project you’ll face some key questions such as: Xamarin.Forms or Native UI? What architecture and/or frameworks are appropriate? How should I persist the state of my applications? What is the best tooling to test, build and deploy my apps? The choices you make here can be the difference between making a real impact in the early stages of your project and being left reinventing the wheel.

Advice for Beginners

As we walk through the early stages of a new Xamarin project, i’ll recommend some options that aren’t necessarily my personal choices right now. Many of these decisions are subjective and depend on the specific context you work in. My advice here is aimed at maximising the output from your early development efforts while minimising the effect of any changes you make further down the line as your experience with Xamarin grows.

So let’s start right at the beginning:

1. Xamarin.Forms or Xamarin native?

When you create a new Xamarin project, your IDE presents you with an intimidating range of options that can be reduced down to a single choice. Do you target one or more specific platform UIs with a standard Xamarin project, or use the Xamarin.Forms framework to build a shared XAML UI that can target multiple platforms? It turns out that the answer is not straight-forward and i’ve explained why I would advise beginners to start with Xamarin.Forms in a separate post.

Recommendation for beginners: If in any doubt use Xamarin.Forms. Follow the optimisation tips suggested by lead Forms developer Jason Smith.

2. Portable libraries or shared code?

Before we can start coding, the second step of a new project asks us to decide how we want to share code. Shared libraries simply allow multiple projects to reuse the same physical code files. They produce no compiled output and typically result in the use of #ifdef compiler directives to include/hide platform specific code. For anyone concerned about clean code principles, separation of concerns, readability or testability, it is very hard to make a good case for shared libraries. Although some have suggested that with careful use of partial classes, they can be made to work well.

The alternative, Portable Class Libraries allow you to target a particular set of platforms and build against a subset of the .Net Framework that is the intersection of all .Net APIs on those platforms. While this might initially sound limiting, the standard techniques used to execute platform specific behaviour through portable code soon become second nature and Xamarin library authors make heavy use of the NuGet bait and switch trick  to allow you to add third party components that give the appearance of enabling platform features through portable code.

Recommendation for beginners: Do it right from the start with Portable Class Libraries. You’ll be ready for the next evolution of PCLs – .Net Standard Libraries (which target a wider, curated set of APIs rather than a common subset). Portable code is only going to get better.

3. What kind of architecture is appropriate?

With two decisions down, we can get on with creating our app, but before we go too far, we need to think about how we will organise our code. By far the most common UI pattern out there for Xamarin apps is MVVM (Model-View-ViewModel). MVVM was designed with XAML in mind and Xamarin.Forms supports data-binding out of the box.

Using a navigation abstraction can allow you to move your page navigation logic into your ViewModels, ensuring it is testable and part of your portable core – in case you ever move away from Xamarin.Forms or target a non-Forms supported platform.

Having committed to portable code we are led towards using Dependency Injection to provide platform specific behaviour and I recommend thinking of your Xamarin app in terms of Hexagonal Architecture.

Recommendation for beginners: Use MVVM with Dependency Injection and ViewModel navigation.

4. What frameworks are useful?

Having identified our target architecture, is there anything out there that can help us reach that goal? Xamarin.Forms is an MVVM framework itself, but there are many quality third-party options that we can turn to to help us out.

MVVM Frameworks typically provide data binding, IoC, messaging and other useful abstractions. Popular examples include Reactive UIMVVMCrossFreshMVVM and Prism. However, to reach our goal of maximising your early efforts while keeping your options open, I suggest using some basic features of the MVVM Light toolkit until you are confident enough to commit to one of the more heavyweight frameworks.

We can use MVVM Light to introduce ViewModel navigation and dependency injection as it comes bundled with SimpleIoc. You will almost certainly want to move on to something more advanced further down the line, but this will get you going quickly. Check out the code sample to see how everything discussed so far comes together.

Recommendation for beginners: Use some basic elements of MVVM Light that will help you move towards the target architecture described above.

5. How should you persist the state of your apps?

SQLite-net is a great third-party Xamarin compatible library that is widely used to enable cross-platform SQLite database access. Although earlier this year a new option arrived on the scene – RealmMany people were quick to move over to Realm which claims to be fast and easy to use, although some have criticised it for lack of an async interface and being difficult to abstract.

So that makes SQLite the obvious choice then? Well, not necessarily. For me, the best talk at Xamarin Evolve 2016 was Paul Betts explaining how it is easy to get your SQLite implementation wrong and take a performance hit as a result. His library Akavache is a simple key/value store based on SQLite that can easily serialise objects to disk, handle secure data or manage caching and expiry of temporary data.

For an even more straightforward option, check out the Settings Plugin from James Montemagno.

Recommendation for beginners: How often do mobile apps need a full-blown relational database? Pull in Akavache via NuGet to easily serialise your objects.

6. How should you test your apps?

The architecture described earlier gives us unit-testable business logic, view models and navigation. With the Hexagonal Architecture in mind, you can create a test configuration of your application to allow you to integration test common-code use cases.

If you find that you want to automate the testing of the user interface or the platform features running on a device, Xamarin Test Cloud allows you to write C# tests and run them against pretty much any device you can imagine in Xamarin’s test lab. I’ve used Test Cloud extensively in the past and would strongly recommend it. Use a Risk-Based approach to judge what level of testing is right for you.

Recommendation for beginners: Start out with unit and integration tests through a test configuration of your core app. If you want to take it further, Xamarin Test Cloud is a great option.

7. What build/deploy tooling should you use?

Wouldn’t it be great if every time you make a change to your apps, an automated process builds, tests and distributes the latest versions to stakeholders? As a Xamarin developer, you have a growing list of options for Continuous Delivery of your applications. It was recently announced that Xamarin Test Cloud and Hockey App will join forces to become Visual Studio Mobile Center.

In the meantime, you have options such as Visual Studio Team Services and AppVeyor to build your apps in the cloud – although for both of these cases you’ll need to provide your own Mac build slave.

Jenkins is always a popular choice if you want to do the grunt work yourself, but as we are looking for the best quick start option here, I suggest you take a look at Bitrise. Bitrise will let you build Android and iOS apps in the cloud for free initially, and has a simple distribution mechanism built-in. If you want more control, there are tons of open source integrations, such as with Hockey App or TestFlight, or you can write your own.

Recommendation for beginners: Bitrise takes seconds to configure and can provide a quick and easy low-cost option for build and distribution of apps.

Summary

For a beginner, i’d recommend using Xamarin.Forms and Portable Libraries. Hand pick some MVVM Light components to help you build your basic MVVM architecture and use Akavache for a lightweight persistence store. Integration test your common core and build and distribute with Bitrise. This combination should get you writing professional apps quickly and with minimal fuss.

But it also leaves the door open for you to take your own direction as you learn more. Keep an open mind about building native UIs. Consider whether a different MVVM framework better suits your needs. Assess whether you want to adopt Xamarin Test Cloud or whether Visual Studio Mobile Center is going to become your best option for the future.

This is just my personal opinion on a quick-win recipe for getting started. Every situation is different and where you take it from there is up to you.

 

This is a summary of my recent talk of the same name. You can find the code sample on GitHub.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s