Code Sharing Strategies2:54 with Heath Hodgert
The benefit of the Xamarin platform is sharing code between multiple platforms so we'll discuss some of the ways that is accomplished. There are patterns such as dependency injection (DI), model-view-viewmodel (MVVM) and factories that can help us with code reuse. Our simple app will be using a simple service pattern to share our pizza calculating algorithm.
[MUSIC] 0:00 We can take a look at the architecture of the project, and 0:04 how we can share code across platforms. 0:08 The basic idea is that we have a project for each platform, so 0:11 the correct MSBuild targets are used. 0:15 We'll also have a library project that has a reference for 0:18 each of these projects to share code between them. 0:21 We choose to use portable class libraries, but we could have used shared libraries. 0:25 I'll explain the difference between the two types. 0:30 Shared libraries are not compiled on their own, instead, 0:35 they're compiled with the referencing project. 0:39 This makes it very easy to write the code once, but 0:42 it gets difficult when we want to do something platform specific. 0:45 With shared libraries, doing something platform specific requires 0:50 compiler directives, or partial classes which are hard to maintain. 0:54 Also, it can be challenging to understand what is actually running on each platform. 0:59 There is also no way to unit test shared library code 1:05 without building a Windows app that references the library. 1:08 Portable Class Libraries allow us to target specific platforms and 1:13 ensure that we only use supported APIs. 1:18 The way this is done is by using a profile, 1:22 which specifies which base class libraries are used. 1:24 In the settings for 1:29 the PCL project, the library target dialog shows platforms you can support. 1:29 And when you pick them, 1:36 it determines which profile is needed to support that combination. 1:37 By using a PCL, 1:41 we don't use compiler directive to switch between platform specific code. 1:43 We'll use interfaces to inject platform specific implementations. 1:47 Also, unit testing becomes easier, 1:52 because you can test the PCL directly without the platform specific libraries. 1:55 PCL is currently the preferred choice, 2:01 although using .NET standard will soon replace PCLs. 2:03 .NET standard is similar to PCLs but 2:07 it's a defined set of APIs that must be supported by the platform. 2:11 So you don't need to select the platform to support 2:16 because if it adheres to the standard, it must be implemented. 2:19 Xamarin is currently .NET standard but 2:24 many of the third party components have not been updated to support it. 2:27 It's not currently part of the mobile project template in Visual Studio, so 2:31 it requires a little bit more work to get started. 2:36 As we continue the course I will refer to all three of these project types, 2:39 as shared projects. 2:44 Also, since we are using PCL projects, 2:46 I will not covered the shared project type implementation differences. 2:49
You need to sign up for Treehouse in order to download course files.Sign up