C# code sharing across the platforms
Andrei Marukovich
LunarFrog.com
twitter: @amarukovich
.NET ecosystem
.NET Framework .NET Core Xamarin
WPF
ASP.NET
WinForms UWP
ASP.NET Core
iOS
macOS
Android
Code sharing strategies
• Source file links
• Shared projects
• Portable class libraries (PCLs)
• .NET Standard
Shared projects
• Better version of the source file links
UWP project Xamarin project
File1.cs
File2.cs
SharedLib
File1.cs
File2.cs
SharedLib
File1.cs
File2.cs
SharedLibUI
App layer
UI
App layer
Shared projects
Shared projects
• Benefits
• Simple model
• May include platform-specific code
• Disadvantages
• Source-level sharing, no assembly
• Compiler directives
• Usability
Portable class libraries
• Sharing code in binary form, without recompiling
UWP project Xamarin project
File1.cs
File2.cs
PortableLib
UI
App layer
UI
App layer
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 AssembliesMicrosoftFramework.NETPortable
UWP
Profile API surface
Xamarin
Android
.NET
Framework
PCLs
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
.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
.NET Standard
.NET platform support
.NET Standard 2.0
Big picture
.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
More information
• github.com/dotnet/standard
Thank you!
Andrei Marukovich
contact@lunarfrog.com
twitter: @amarukovich

C# code sharing across the platforms

  • 1.
    C# code sharingacross the platforms Andrei Marukovich LunarFrog.com twitter: @amarukovich
  • 2.
    .NET ecosystem .NET Framework.NET Core Xamarin WPF ASP.NET WinForms UWP ASP.NET Core iOS macOS Android
  • 3.
    Code sharing strategies •Source file links • Shared projects • Portable class libraries (PCLs) • .NET Standard
  • 4.
    Shared projects • Betterversion of the source file links UWP project Xamarin project File1.cs File2.cs SharedLib File1.cs File2.cs SharedLib File1.cs File2.cs SharedLibUI App layer UI App layer
  • 5.
  • 6.
    Shared projects • Benefits •Simple model • May include platform-specific code • Disadvantages • Source-level sharing, no assembly • Compiler directives • Usability
  • 7.
    Portable class libraries •Sharing code in binary form, without recompiling UWP project Xamarin project File1.cs File2.cs PortableLib UI App layer UI App layer
  • 8.
    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 AssembliesMicrosoftFramework.NETPortable
  • 9.
  • 10.
  • 11.
    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
  • 12.
    .NET Standard • Anevolution 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
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
    .NET Standard • Currentstate • 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
  • 18.
  • 19.