Saturday 25 June 2016

The Best Things That Happens to Desktop App Developers- Project Centennial



In Build'16 Microsoft announced a technology which is going to convert the desktop applications into the Universal Windows Applications and this technology is going to be shipped with Windows 10 Anniversary update which is about to be released soon. This technology is known as the "Desktop Bridge" or "Project Centennial" and it will be the best thing that will happen for the desktop application developers who wants to use all the features that are available for Universal Windows platform like Cortana and live tiles. Now you can prepare your applications like Win32,WPF to Universal Windows Applications by using the Desktop Bridge (Desktop App Converter).


So in this discussion I am going to explain why Microsoft wants this bridging between desktop and UWP and we are also going to dive into the process of how it will work. So let's start rolling:


 Why Microsoft Wanted this Project ? 


Making Windows Store better:


There are a huge number of developers who just learn the C# or Vb.net like me and start developing desktop applications and the number of desktop applications till now are more than 16 million. Now when Microsoft started shifting towards bringing all the technologies onto one platform they just skipped this part of 16 million+ desktop applications and the desktop developers were like Oooooo gooosh!! What we can do to take advantage this universal platform and the features involved in it. Many developers surely thought about learning new technologies and have done some hack or custom logics to creat the live tiles etc. Now here's the main reason. Which developer will not want to have his application running across all the devices Microsoft has created like Hololens , Windows phone etc and more importantly Windows 10 is running on billions of devices and image what would be the impact if we can convert those 16 million+ desktop applications to the Universal Windows Platform yielding a great and bulky Windows Store and a great opportunity for the Windows Form/Desktop Developers to have their Application accessible across those billions of devices. So finally Microsoft designed a technology which is for taking care of the missing part of millions of desktop applications. The project name is Project Centinniel or Desktop Bridge and in the Anniversary update of Windows 10 it will be available for all the developers. 

Easy Installation and Un-Installation of Application:


When we bought a new Laptop/ Personal computer, it runs so fast. It boots fast, it opens every file very fast and all of the features are pretty much fast. But with the time, it get slowed and all of the functionality which was very fast, periodically get slow down. Why is the reason for that? You will get this answer shortly. 

When you install an application to the desktop through an installer, what the installer does is that it goes to the registries of the Windows and add some new files there and then there is an Application Folder App_Data it includes all the files that the application stores while running and there are some other files which get piled into the memory. Now When we re-install an application, It is impossible to remove all of the files associated with that application and even if you want to remove those let's say we want to remove the registries of that application, the operating system will give you a big dialog showing a dialog box saying like if you delete or modify this registry you will never be able to reboot or blah blah blah your System. So what the system eventually has to do is that when the system reboots it has to load all those registry files and it starts taking time giving you a latency period in the functionalities. For Example Every developer would have installed Visual Studio at any time, this studio saves about 1 million registry files in the system and it's around 30 Megabytes of just registries. 
As compared to the Desktop Applications , UWP Applications has nothing to do with all of the above mentioned process and their Un-installation removes the whole application from the system leaving no extra files because the Universal Windows Applications has all of these files in their respective folders , So removing those isn't a big issue. So this is how the Desktop applications when being converted to the Universal Windows Applications are going to be left with no extra files not causing the System to have the latency. I will discuss it later how does it happens. 


Solving Performance Issues


Desktop applications don't have an application life cycle events just like the Universal windows Applications have. Like onNavigatedTo and onNavigatedFrom. So let's create a mock Scenario. You have an desktop application running which is doing some heavy CRUD operations using Entity Framework and when you put your computer to sleep, what actually happens is that your desktop application also goes to sleep with the system staying in memory. Now notice that there is not mechanism that tells the application that our system is going to sleep so please release the resources or in this case close out all the connections of the Database. So UWP address this problem. The applications there has an Application Lifecycle which can determine if the application is paused or stop etc and the we can perform the related operations accordingly resulting in the less usage of the resources and giving some extra performance. 

Runtime Memory Optimization:


As stated above, in case of Windows Desktop Applications the system when reboots it has to load all the registry files first. In Case of  UWA,  Applications works all with the registry files when they are started not when the system is started. So UWP offers the runtime optimization as compare to the desktop applications. So this gives Microsoft another strong reason to have this project underway. 


Deep Dive Into Working of Desktop Bridge:


I know till now there will be some obvious questions and some strange feelings about how it will fix the registries or how Universal Windows Applications will allow the third party components to get embed into it. All of these questions will be answered when you will understand the working of this project. 

Now there are basically three main steps of converting your Application using the Desktop Bridge:
  • Convert the Application using the Desktop Bridge
  • Test your Converted Application
  • Deploy your Application
Sounds pretty much. The first step is solved through some small scripts in the power shell provided by the Desktop Bridge. The third and second steps do not have  so much to discuss about. So let's discuss about the first step and reveal the hidden secrets behind it. 

Conversion of Application:


The desktop bridge in simple converts the Win32 application into the AppX Package. The AppX package consists of all the files for the deployment of the UWA. Now let's discuss about how the registry stuff yielding the performance upgrade will happen. Keep in mind the App should be signed before the conversion means the App should have digital certificates just for the security purposes. 

  • The AppX package created by the Converter consists of an file known as Registry.bat. This will be the file which will be responsible for all the handling of the registry files. Now what will happen when the process of registries will come into play. The files in the AppX package are mock files. Understand the work flow now. The registry files will first go into the same place as all the other registries but the developers of this bridge tricked it there and re-direct those registries to the AppX package. The system thinks that the registries are saved in the same place as others but actually it is in the folder. You can just think it of as Virtualization. Same goes for the App_Data folder. Creators tricked this part too and re-direct the system to the same folder resulting in the data at just one place and deletion of data will be only from one place. 
  • After the conversion process now there are two processes in AppX. One is the primary desktop application and the other is the Universal Windows Application running in the App Container. Now there are two possibilities. Either the Desktop Application can communicate with the Universal Windows Application or the Universal Application can communicate with the Desktop Application. Now How it is going to happen. Actually Microsoft has already a solution for this. So it is already built and the Solution is "App Service". Using the App services , it can result in the bi-directional communication between the two. Now here's your answer coming about how the external Dll's will be used in the UWP and that will be through the App Service resulting in the application. I don't have a demo for this but I will Surely show that demo after the converter will be shipped. If the UWP will be wanting to use any third party application it will ask the desktop application to perform it and UWP can then process the final result. 
  • Though after the two processes conversion, The windows store will not allow to deploy an application that has further some applications installed in it. So after having the two process we have to create one process. Yet I don't have any clue about how to do it but Microsoft is working on All the API's which are not allowed by the developers to use in the UWA's. 
  • After this all process, the same Win32 Application can leverage the benefit of background tasks, Cortana,Live Tiles and able to run on billions of devices. 
  • Every Application that is converted into the UWP will get an App Identity for registration of being able to send push notifications and live tiles. The App Model for the installation and Un-installation of the application. 

Minor Disadvantage:

There is just one disadvantage of this Desktop App Converter I have So far pointed out is that the some Win32 Applications are definitely going to use some drivers and going to install those into your system while installing. The Converted UWP Application will just not be able to install the drivers but will be able to use those if those are already installed.



The App Converter will soon be released may be in upcoming month and the thing that should be kept in mind that it will be the first version of this converter and Microsoft will keep on adding more features to this converter and all of the related problems can't be answered by the first release. It will take some time to mature and it will be definitely the best thing for the Desktop App Developers ever going to happen. 

For the Sake of Information Saga2000 was the first desktop Application which was converted using the desktop bridge to the Universal Windows Platform and got the privileges of having the features of UWP.




Tuesday 21 June 2016

Language Integrated Query (LINQ) To Objects

Language Integrated Query is an innovation introduced in .Net 3.5 with a vision towards bridging the gap between world of object and world of Data.

Before the LINQ, developers have to learn a different query language. Like for the XML documents dev's have to work  classes like XmlWriter,XmlReader and similarly for the SQL DataBases dev's have to work with SqlConnection,SqlCommand and so on. So Linq removed this difficulty of learning different query language for each DataSet.

We can write LINQ queries in C# for all the available datasets like SQL databases, XML documents, various Web Services  and for any other collection of objects which is implementing the IEnumberable Or IEnumerable<T> interface. See MSDN for the Reference. 

Moving towards the implementation of the LINQ, LINQ offers different flavors.

  • LINQ TO DataSets
  • LINQ TO Entity Framework
  • LINQ to Objects
  • LINQ to XML


Moreover there are two types of writing the LINQ queries.
1. Linq Query Syntax
2. Linq through Extension Methods

In this blog I am only focusing on Linq Query Syntax and not on Linq Through Extension Methods. 

BASIC QUERY OPERATORS:

Before diving into the implementation of the LINQ to objects lets have a look into the LINQ Query operators that are available for different operations. 

There are three different basic steps for writing a LINQ Query. The code samples are just to give an overview of the LINQ operators. 


1. Obtaining a Data Source:

In a LINQ query, the first step is to specify the data source. In C# as in most programming languages a variable must be declared before it can be used. In a LINQ query, the from clause comes first in order to introduce the data source (customers) and the range variable (cust).



The variable "queryAllCustomers" is the result of the above mentioned query and after applying the foreach loop the results can then be extracted. (Discussed Later). The range variable is like the iteration variable in a foreach loop except that no actual iteration occurs in a query expression. When the query is executed, the range variable will serve as a reference to each successive element in customers. Because the compiler can infer the type of cust, you do not have to specify it explicitly

2. Create the Query:
Creating the query includes performing different operations after obtaining the data source. Basic query operations involves the following with examples:


a. Filteration of data
Probably the most common query operation is to apply a filter in the form of a Boolean expression. The filter causes the query to return only those elements for which the expression is true. The result is produced by using the where clause. The filter in effect specifies which elements to exclude from the source sequence. In the following example, only those customers who have an address in London are returned.






You can also apply logical operators in these queries like && and || and it will work just fine as the normal scenarios. For more information please explore "Where Clause" in MSDN. 

b. Ordering:
Often it is convenient to sort the returned data. The orderby clause will cause the elements in the returned sequence to be sorted according to the default comparer for the type being sorted. For example, the following query can be extended to sort the results based on the Name property. Because Name is a string, the default comparer performs an alphabetical sort from A to Z.


If we want to sort the result in the descending order then we just have to replace the ascending with the descending keyword. Intellisense will help you with that.

3. Selecting (Projection) :

The select clause produces the results of the query and specifies the "shape" or type of each returned element. For example, you can specify whether your results will consist of complete Customer objects, just one member, a subset of members, or some completely different result type based on a computation or new object creation.


The select clause in here produces the result and assigns the result to the "queryLondonCustomers3". 
The point to mention here is that if we want to select more than one element/attribute or a set of specific characteristics then a Technique comes in which is know as projection. See the code below:


Notice the "new " keyword here. This new keyword is used to create an anonymous object in here and it will select only the Name and City characteristics of all the customers.

There are other operations like grouping and joining of the Queries but I am not going to discuss that here. Please see MSDN for the reference.



3. Execution of Data:

And at the end We are going to execute the query and for that we are going to use the foreach loop. And it would be done something like this:

Notice we are using foreach loop in here for the execution of query. Reason for that is discussed later. 


LINQ TO OBJECTS:

LINQ To Objects refer to the use of queries for all the collections which implements the IEnumerable or IEnumerable<T> interface like List<T>,List and all other collections including Arrays,Dictionary and so on. 
If you don't know about the IEnumerable<T> or IEnumerable please see the links given at the end. In Fact the Foreach loop in .Net depends completely on IEnumerable. 


Now we have the basic understanding of the Linq queries we are going to apply our above knowledge to the Objects and are going to explore various concepts in this section like Immediate and Deferred Execution. 

  • Create An Console Application in Visual Studio. 
  • Create A Class named Student:




  • Go to the Main.cs and Create a List of the Student and populate that list with the dummy data for just applying the queries. 





  •  Now from here we are going to apply the LINQ Query Operators to retrieve the results. If we don't have these operators we will be prompted to manually search through the List by using conditional statements but LINQ simplifies this hurdle. In first step I am going to obtain the data source and retrieve all the students. 



 The output will be the following as expected: 



That's the power of the LINQ which just writing 2 lines of code we are having all the related data we wanted and same will be the case with all the datasets that are available. 


  •  Now Applying filters. Let's say we have want to retrieve a student which belongs to the city "Sialkot" so for that we are going to use the where clause. 



The result we will be having will be :





5. Now we are going to sort the students according to the names of the students and you can then sort accordingly to your own requirements. So let's have an example:

And the output will be then be as follows:







Sorting depends upon the default comparer as implemented. If you are willing to implement the default comparer in your own implemented classes then see the IComparer and IComparable Interface. 


  •  Now Finally we are going to talk on Projection or selecting the data. you have seen above the select clause which is just selecting the whole instance of the student and you get all of the information of the student. But let's say we are not interested in the whole information and we want to just have the Name of the students. So what's we going to do that? Here's the concept comes which is known as the Projection and that is achieved through the help of anonymous types. See the code: 






Now we are using the "New " keyword here and using the anonymous types here to select specific data of the student is known as the projection. We can the following results:



Now instead of having the whole instances of the class we just have the Name and the fathers Name of the students. See MSDN for the reference ( Anonymous types) .

So this is how the LINQ eases the way to retrieve the results from the objects and just by learning some operators we can apply on different datasets easily. Either that is XML or SQL or any.



Immediate Execution and Deferred Execution:

Now as you will notice in above all of the scenarios, that we are using foreach loop every time. The reason for this is that the LINQ query will be executed when we will apply foreach loop over the selected results. For supporting my argument I am going to Add a new student in the class after applying query but before applying foreach Loop and you will see the required results. 

Notice that we have added a new student after the query. As a general perception you will be thinking that the student with the name Noman will not be included in the results. But let's see the results. 




Notice the name Noman is included. That's called Deferred Execution. which means the LINQ query will not be implemented unless we apply foreach loop over that. We can also see by putting break point in there. And after pressing F11 "Step into debugging" and the control will again go into the query and will then output the result. 


Now let's see what is immediate execution. Immediate execution doesn't waits for the foreach loop to get executed. Queries that returns a singleton value like Average,Sum and count are implemented before the foreach loop. Lets see through the example. 



This code sample is converting the results into List. And we are again adding the student before showing onto the console. And the result will be;



There should be 11 students. But this is showing that the query is executed before the foreach loop and instead of adding the 11th student in the list, the result is only of 10 students. 

So this is how immediate execution differs from the deferred execution and it can have adverse effects in the enterprise applications. 

Now you have learn the basics of the Linq you should explore other features of LINQ like joining and grouping. And Applying LINQ To SQL and other DataSets should not be a big problem for you.