Upload
andrei-marukovich
View
159
Download
2
Embed Size (px)
Citation preview
C# code sharing across the platforms
Andrei MarukovichLunarFrog.comtwitter: @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.csFile2.cs
SharedLib
File1.csFile2.cs
SharedLib
File1.csFile2.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.csFile2.cs
PortableLib
UI
App layer
UI
App layer
ReferencesReferences
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\
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