Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

C# code sharing across the platforms

408 views

Published on

Portable, shared, net-standard libraries – so many options to choose when you need to share some code between the platforms. During this talk we will explore all the options and differences between the library types. After the session you will have a solid understanding of the modern .NET library types and code sharing strategies which you can apply for your next .NET Core, desktop or Xamarin project.

Published in: Software
  • Be the first to comment

  • Be the first to like this

C# code sharing across the platforms

  1. 1. C# code sharing across the platforms Andrei Marukovich LunarFrog.com twitter: @amarukovich
  2. 2. .NET ecosystem .NET Framework .NET Core Xamarin WPF ASP.NET WinForms UWP ASP.NET Core iOS macOS Android
  3. 3. Code sharing strategies • Source file links • Shared projects • Portable class libraries (PCLs) • .NET Standard
  4. 4. 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
  5. 5. Shared projects
  6. 6. Shared projects • Benefits • Simple model • May include platform-specific code • Disadvantages • Source-level sharing, no assembly • Compiler directives • Usability
  7. 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. 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. 9. UWP Profile API surface Xamarin Android .NET Framework
  10. 10. PCLs
  11. 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. 12. .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
  13. 13. .NET Standard
  14. 14. .NET platform support
  15. 15. .NET Standard 2.0
  16. 16. Big picture
  17. 17. .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
  18. 18. More information • github.com/dotnet/standard
  19. 19. Thank you! Andrei Marukovich contact@lunarfrog.com twitter: @amarukovich

×