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.

Saturday, December 14, 2013

Visual studio 2013 community launch by BDotnet and Microsoft

Today I attended an event at MS office in bangalore where VS 2013 was relaunched infront of around 130 enthusiastic bdotnet members.

It started with TEJASVI KUMAR giving an overview of TFS 2013. I liked the code review enhancements, chat room and VS online feature specially.

Then VIC PARMAR gave an much needed overview of VS 2013 IDE features. The quicklaunch and the synchronization feature were awsome.

Then came the keyboard expert RAJASEKHARAN VENGALIL. As always he showed awsomeness in web feature enhancements like one ASP.net and web api 2.

After a nice launch the session by KARTHIKEYAN ANBARASAN gave knowledge on azure. came to know that MS gives $150 credit for month for a developer to use azure services !!!

Then came Ujjwal. He gave nice ideas on how a modern app should be.He showed new enhancements in Windows 8.1 apis.I came to know how xamarin tool can be used for multi platform apps.

Then cam energetic Lohith. He gave an overview of "Monaco". He showed how azure website can be edited from an web interface. It is awsome. We can edit websites on the fly without opening VS.

Overall a day well spent.
Please join BDOTNET if you are still not.
https://www.facebook.com/groups/BDotNet/


Monday, December 3, 2012

A Comparison of IComparable and IComparer interfaces in C#




.Net framework provides 2 interfaces (IComparable and IComparer) for purpose of sorting/Comparing . 

IComparable <T>:

This interface is to be implemented by the class itself whose instances need ordering/sorting .It provides CompareTo method to implement.
All .net primitive types like Int32,String etc. implements IComparable.So we can easily compare 2 built in types using CompareTo method.
Eg.
  int i = 9;
  int j=10;
  i.CompareTo(j);
Also if we have a collection of a built in type we can sort the collection easily.

Eg.
List<int> lstInt = new List<int>();
lstInt.Sort();

Now say we have a custom type Employee. Lets try the following
 List< Employee > lstEmp = new List< Employee >();
…………………………………….
Add some employees
…………………………………
lstEmp.Sort();

Now when we try to do sorting on a custom type we will get exception “Failed to compare two elements in the array.”
It is because to use sort() on anytype , the type has to implement IComparable .Now lets try by implementing IComparable .
e.g.
  public class Employee:IComparable<Employee>
    {
        public string Name { get; set; }
        public int JanSalary {get;set; }
        public int FebSalary { get; set; }

       
        #region IComparable<Employee> Members

        public int CompareTo(Employee other)
        {
            return this.Name.CompareTo(other.Name);
        }

        #endregion
    }

public class Comparison1
  {
      List<Employee> lstemp = new List<Employee>();
      public void docomparison()
      {

          Employee emp1 = new Employee();
          emp1.Name = "satya";
          emp1.JanSalary = 2000;
          Employee emp2 = new Employee();
          emp2.Name = "biswa";
          emp2.JanSalary = 300;
          Employee emp3 = new Employee();
          emp3.Name = "parsottam";
          emp3.JanSalary = 1000;
          Employee emp4 = new Employee();
          emp4.Name = "kalyani";
          emp4.JanSalary = 15000;
          EmpJanSalComparer cc = new EmpJanSalComparer();
          lstemp.Add(emp1);
          lstemp.Add(emp2);
          lstemp.Add(emp3);
          lstemp.Add(emp4);
          lstemp.Sort();

      }
  }

Here when we try to sort the list , CompareTo method will be called in Employee class and sorting/ordering can be performed.
We can use LINQ for the same . In that case we don’t have to implement IComparable in “Employee” class.

lstemp.Sort((x,y)=>x.Name.CompareTo(y.Name));

As we have to use IComparable with the class which needs comparison(Employee in above case) , we are restricted to do one type of comparison / ordering only(in this case on basis of property “Name“).

Suppose I need comparison on basis of “JanSalary” then I can’t do it now with IComparable .



IComparer<T> :

For these scenarios we have “IComparer<T>” interface using which we can have our custom comparisons. IComparer is used with a separate class as shown below.
public class EmpJanSalComparer : IComparer<Employee>
    {
        #region IComparer<Employee> Members

        public int Compare(Employee x, Employee y)
        {
            if (x.JanSalary > y.JanSalary)
            {
                return 1;
            }
            else if (x.JanSalary < y.JanSalary)
                return -1;
            else
                return 0;
        }

        #endregion
    }

    public class EmpFebSalComparer : IComparer<Employee>
    {
        #region IComparer<Employee> Members

        public int Compare(Employee x, Employee y)
        {
            if (x.FebSalary > y.FebSalary)
            {
                return 1;
            }
            else if (x.FebSalary < y.FebSalary)
                return -1;
            else
                return 0;
        }

        #endregion
    }


So as we see   it has a method “Compare” which takes 2  parameters of type “T” .And it is used with a separate class and we can use many of these classes as needed. So it gives flexibility/extendibility in terms of comparison/sorting.
Here for comparing jan month salary we used  EmpJanSalComparer” class and for comparing feb month salary we used “EmpFebSalComparer .We can use these comparer for sorting as shown below.
  EmpJanSalComparer cc = new EmpJanSalComparer();
  lstemp.Add(emp1);
  lstemp.Add(emp2);
  lstemp.Add(emp3);
  lstemp.Add(emp4);
  lstemp.Sort(cc);

Hope you enjoyed the article. Please leave a honest comment/feedback.