Skip to content

Instantly share code, notes, and snippets.

@adamchester
Last active April 24, 2016 06:07
Show Gist options
  • Save adamchester/87f730702a1dfce11cf1e9fe228cb436 to your computer and use it in GitHub Desktop.
Save adamchester/87f730702a1dfce11cf1e9fe228cb436 to your computer and use it in GitHub Desktop.
How .NET Standard relates to .NET Platforms (in F#)
// Converted to F# from https://gist.github.com/davidfowl/8939f305567e1755412d6dc0b8baf1b7
module Analogy =
type ASetOfApis = unit -> unit
// Each interface represents a target framework and methods represents groups of APIs available on that target framework.
// The goal is to show the relationship between .NET Standard API surface and other .NET platforms
// .NET Standard
type INetStandard10 =
abstract member Primatives : ASetOfApis
abstract member Reflection : ASetOfApis
abstract member Tasks : ASetOfApis
abstract member Collections : ASetOfApis
abstract member Linq : ASetOfApis
type INetStandard11 =
inherit INetStandard10
abstract member ConcurrentCollections : ASetOfApis
abstract member InteropServices : ASetOfApis
type INetStandard12 =
inherit INetStandard11
abstract member ThreadingTimer : ASetOfApis
type INetStandard13 =
inherit INetStandard12
abstract member FileSystem : ASetOfApis
abstract member Console : ASetOfApis
abstract member ThreadPool : ASetOfApis
abstract member Process : ASetOfApis
abstract member Sockets : ASetOfApis
abstract member AsyncLocal : ASetOfApis
type INetStandard14 =
inherit INetStandard13
abstract member IsolatedStorage : ASetOfApis
type INetStandard15 =
inherit INetStandard14
abstract member AssemblyLoadContext : ASetOfApis
// .NET Framework
type INetFramework45 =
inherit INetStandard11
abstract member FileSystem : ASetOfApis
abstract member Console : ASetOfApis
abstract member ThreadPool : ASetOfApis
abstract member Crypto : ASetOfApis
abstract member WebSockets : ASetOfApis
abstract member Process : ASetOfApis
abstract member Sockets : ASetOfApis
abstract member AppDomain : ASetOfApis
abstract member Xml : ASetOfApis
abstract member Drawing : ASetOfApis
abstract member SystemWeb : ASetOfApis
abstract member WPF : ASetOfApis
abstract member WindowsForms: ASetOfApis
abstract member WCF : ASetOfApis
type INetFramework451 =
inherit INetFramework45
inherit INetStandard12
// TODO: .NET Framework 4.5.1 specific APIs
type INetFramework452 =
inherit INetFramework451
inherit INetStandard12
// TODO: .NET Framework 4.5.2 specific APIs
type INetFramework46 =
inherit INetFramework452
inherit INetStandard13
// TODO: .NET Framework 4.6 specific APIs
type INetFramework461 =
inherit INetFramework46
inherit INetStandard14
// TODO: .NET Framework 4.6.1 specific APIs
type INetFramework462 =
inherit INetFramework461
inherit INetStandard15
// TODO: .NET Framework 4.6 specific APIs
// Mono
type IMono43 =
inherit INetFramework46
abstract member MonoSpecificApi : ASetOfApis
// Windows Universal Platform
type IWindowsUniversalPlatform =
inherit INetStandard14
abstract member GPS : ASetOfApis
abstract member Xaml : ASetOfApis
// Xamarin
type IXamarinIOS =
inherit INetStandard15
abstract member AppleAPIs : ASetOfApis
type IXamarinAndroid =
inherit INetStandard15
abstract member GoogleAPIs : ASetOfApis
// .NET Core
type INetCoreApp10 =
inherit INetStandard15
// Future platform
type ISomeFuturePlatform =
inherit INetStandard13
// A future platform chooses to implement a specific .NET Standard version.
// All libraries that target that version are instantly compatible with this new
// platform
module Samples =
open Analogy
/// This example shows that a library that needs access to target .NET Standard 1.3
/// can only access APIs available in that .NET Standard. Even though similar the APIs exist on .NET
/// Framework 4.5, it implements a version of .NET Standard that isn't compatible with the library.
/// </summary>
type Example1 () =
member this.Net45Application (platform : #INetFramework45) =
// .NET Framework 4.5 has access to all .NET Framework APIs
platform.FileSystem()
platform.Console()
// This fails because .NET Framework 4.5 does not implement .NET Standard 1.3
// this.NetStandardLibrary13(platform)
// error FS0001: The type 'INetFramework45' is not compatible with the type 'INetStandard13'
member this.NetStandardLibrary13 (platform: #INetStandard13) =
platform.FileSystem()
platform.Console()
/// This example shows a library targeting multiple frameworks and 2 different applications
/// using that library. MultipleTargetsLibrary needs access to the FileSystem, that API was only available
/// in .NET Standard 1.3. .NET Standard 1.3 only works with .NET Framework 4.6. Because of this
/// MultipleTargetsLibrary needs to add support for .NET Framework 4.5 explicitly.
type Example2() =
// On the .NET Core 1.0 application, the INetStandard13 implementation is choson
member this.Net45Application (platform: #INetFramework451) = platform.FileSystem()
member this.NetCoreApplication (platform: #INetCoreApp10) = platform.FileSystem()
/// This example shows how future platforms can be added without the need to change libraries that
/// target the .NET Standard. JSON.NET targets .NET Standard 1.0 and can run on *ANY* platform that
/// implements the standard.
type Example3 () =
/// You are about to use JSON.NET on a future platform without recompiling it
member this.FuturePlatformApplication (platform: #ISomeFuturePlatform) = this.JsonNet platform
/// Represents an implementation of JSON.NET which supports .NET Standard 1.0
member this.JsonNet (platform: #INetStandard10) =
platform.Linq()
platform.Reflection()
platform.Collections()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment