Monday, February 20, 2017

.Net Standard versioning and PCL comparision


In previous post, we have learned how to create simple .Net Standard library using VS 2015 and VS 2017 RC. Today we will discuss how .Net Standard is different from PCL and how .Net Standard is versioned.

PCL is an afterthought. So, individual platforms such as .Net Core, Xamarin etc. is built and developed against their base libraries. When code reuse is needed, we use PCL which takes an intersection of the base libraries of individual platforms.

This has lots of issues like what happens when tomorrow your PCL needs to support a new platform?
You create a new PCL profile and take again intersection of all the platforms involved. This means recreate the library again with a different profile. Hence a new PCL will be created with intersection of the platforms and hence lot of changes needed in the already written library as the base library is now further reduced in terms of APIs.

On the other hand, .Net Standard is a before thought. Platforms in .Net develop against .Net Standard. So tomorrow your .Net Standard library can be consumed by any new platform as long as that platform supports that version or a higher version of .Net Standard.

Let’s now see more on .Net Standard versions. Below are 2 rules.

Higher the version, Larger is the API set.
Lower the version, more number of platforms supports that version of .Net Standard.

Currently they are versioned as 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0. So, version 1.2 has all APIs introduced in 1.2 version along with all APIs from 1.0. Similarly, 2.0 has all APIs from 1.6 plus APIs introduced in 2.0.
1.6 is the stable version and 2.0 is the latest release candidate as of writing this article.

Mapping PCL Profiles to .Net Standard:

As we know PCL has different profile numbers. For example, if PCL supports .NET Framework 4.5, Windows 8 then its profile 7.

Based on the profile number we can convert a PCL to a specific version of .Net Standard.

PCL Profile
.NET Standard
PCL Platforms
7
1.1
.NET Framework 4.5, Windows 8
31
1.0
Windows 8.1, Windows Phone Silverlight 8.1
32
1.2
Windows 8.1, Windows Phone 8.1
44
1.2
.NET Framework 4.5.1, Windows 8.1
49
1.0
.NET Framework 4.5, Windows Phone Silverlight 8
78
1.0
.NET Framework 4.5, Windows 8, Windows Phone Silverlight 8
84
1.0
Windows Phone 8.1, Windows Phone Silverlight 8.1
111
1.1
.NET Framework 4.5, Windows 8, Windows Phone 8.1
151
1.2
.NET Framework 4.5.1, Windows 8.1, Windows Phone 8.1
157
1.0
Windows 8.1, Windows Phone 8.1, Windows Phone Silverlight 8.1
259
1.0
.NET Framework 4.5, Windows 8, Windows Phone 8.1, Windows Phone Silverlight 8


So, if your PCL is of profile 151, then you can convert it to support .Net Standard 1.2.

How to create a .Net Standard Library

In previous post, I had explained what is .Net Standard. Let’s now use visual studio and see how to build a simple .Net Standard Library.

Using VS 2015:

Creating a .Net Standard library using VS 2015 is not straight forward. Below are the steps.

Create a Portable Class Library and choose any selection of platforms you want
 

Right click on the PCL project in Solution explore and go to properties













As you can see there is an option to change this PCL to a .Net Standard library. Click on it and select yes in  the prompt.











Now this library is .net Standard Library and not a PCL library anymore.










You can see it is using .Net Standard 1.4.

Using VS 2017 RC:


Its straight forward in VS 2017. Create a .Net Standard Library as shown below













The option is directly there when creating a class library.

Now create a simple .Net Standard Library and consume it in .Net core application and a WPF application. We just need to add reference to the .net Standard library as we do for other libraries.

In next post I will explain how .Net Standard library differs from PCL and versioning in .Net Standard Library. Stay tuned.


Sunday, February 19, 2017

What is .Net Standard and basic Purpose of .Net Standard

.Net Standard:

.NET Standard is a standard (like html 5 standard) and it has a set of APIs that all .NET platforms have to implement.

Let’s understand this a bit deeper with a scenario.

Let’s say I am building an application and it is based on WPF. So, it will use some version of .net framework as its base class library(BCL). Now let’s say the requirement is to develop a web version of the same and the team want to built it in ASP.Net core. Also, lets say after some days the team now want built an iOS app for the same application.
So now the application has 3 flavors. WPF, ASP.NET Core and iOS app

Below are few issues in above scenario:
  • Here clearly the core business of the application will be similar across different platform. Hence, we expect the business library written for one platform to be reused to maximum in other two platforms. Here library means the core business logic. However, we cannot do much code reuse here. The reason is the core libraries of WPF, Asp.Net core and Xamarin iOS app remains different and are not compatible.
  • Below is a nice diagram depicting the above         
     

          As you see above the base libraries of the 3 platforms are different. Hence library written for one platform won't be supported in the others.
  • Also, we need the developer to learn APIs in 3 different libraries to develop same or similar logic.
  • One more side effect is, any changes needed done in one library has to be replicated in all the 3 libraries for uniformity.
.Net standard tries to solve the core problem mentioned in above scenario. It is a specification which all the .net platforms will adhere to.

Below is a diagram depicting .Net Standard.


As you observe all the platforms in .Net world now have their core library as .Net Standard. Hence the issues we discussed such as code reuse, developer learning new apis for new platforms will be eliminated.
So, let’s say tomorrow a new platform X comes in .Net world. So, X will adhere to .Net Standard and won’t have its own base library. Hence any application built using platform X can consume a .Net Standard library and improves productivity.


In next post, I will explain how to create a simple .Net Standard based library using VS 2017 RC. Stay tuned.