19
C# code sharing across the platforms Andrei Marukovich LunarFrog.com twitter: @amarukovich

C# code sharing across the platforms

Embed Size (px)

Citation preview

Page 1: C# code sharing across the platforms

C# code sharing across the platforms

Andrei MarukovichLunarFrog.comtwitter: @amarukovich

Page 2: C# code sharing across the platforms

.NET ecosystem

.NET Framework .NET Core Xamarin

WPF

ASP.NET

WinForms UWP

ASP.NET Core

iOS

macOS

Android

Page 3: C# code sharing across the platforms

Code sharing strategies• Source file links• Shared projects• Portable class libraries (PCLs)• .NET Standard

Page 4: C# code sharing across the platforms

Shared projects• Better version of the source file links

UWP project Xamarin project

File1.csFile2.cs

SharedLib

File1.csFile2.cs

SharedLib

File1.csFile2.cs

SharedLibUI

App layer

UI

App layer

Page 5: C# code sharing across the platforms

Shared projects

Page 6: C# code sharing across the platforms

Shared projects• Benefits• Simple model• May include platform-specific code

• Disadvantages• Source-level sharing, no assembly• Compiler directives • Usability

Page 7: C# code sharing across the platforms

Portable class libraries• Sharing code in binary form, without recompiling

UWP project Xamarin project

File1.csFile2.cs

PortableLib

UI

App layer

UI

App layer

ReferencesReferences

Page 8: C# code sharing across the platforms

How it works?• PCLs are based on profiles• Profile defines an API surface supported by the library• Profile is identified by combination of supported platforms• http://embed.plnkr.co/03ck2dCtnJogBKHJ9EjY• C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\

Page 9: C# code sharing across the platforms

UWP

Profile API surface

Xamarin Android

.NET Framework

Page 10: C# code sharing across the platforms

PCLs

Page 11: C# code sharing across the platforms

Portable class libraries• Benefits• PCLs allow binary distribution• API enforcement• No context switching, simple refactoring

• Disadvantages• PCL’s require upfront decision on the supported platforms• Lowest common denominator for the selected platforms • Cannot use compiler directives• Long term maintainability

Page 12: C# code sharing across the platforms

.NET Standard• An evolution of PCLs• .NET Standard defines a set of APIs to be implemented

by .NET platforms (specification)• .NET Standard is versioned (API level)• Library developers may choose target .NET Standard version• Version defines API surface, not supported frameworks

• The higher the version, the more APIs you have• The lower the version, the more .NET platforms implement it

• Analogy: https://gist.github.com/davidfowl/8939f305567e1755412d6dc0b8baf1b7

Page 13: C# code sharing across the platforms

.NET Standard

Page 14: C# code sharing across the platforms

.NET platform support

Page 15: C# code sharing across the platforms

.NET Standard 2.0

Page 16: C# code sharing across the platforms

Big picture

Page 17: C# code sharing across the platforms

.NET Standard• Current state• Tooling is in preview• Currently PCLs support a wider range of platforms• At the moment, PCL adoption (# of libraries) is higher than .NET Standard

• Future• PCL replacement• Standard 2.0 will have significantly larger API surface• Standard 2.0 will be interoperable with .NET Framework

Page 18: C# code sharing across the platforms

More information• github.com/dotnet/standard

Page 19: C# code sharing across the platforms

Thank you!

Andrei [email protected]: @amarukovich