Differed Execution (vs) Immediate execution in LINQ

Recently while understanding LINQ, many things unfurled from MSDN. The main theme of .NET is lazy binding (or) lazy loading. And it is evolving in all the ways within the .net Framework. I recollect in one of my previous posts about the mainly concentrated theme for every version that is released. I could be wrong from the insider perspective, but what I’ve observed is what I’ve written.

Anyhow, coming back to the main purpose of this post, which is all about the lazy loading and the point behind various execution mechanisms that are available within LINQ. Before I proceed further, one point is very certain that, choosing LINQ is costlier from the point of “Response Time” from DB to any connected system. This statement is true when compared the LINQ with any traditional mechanism of database connectivity.

Traditionally, every developer writes code with Connection object for connecting to the Data Store and then disposes the connection. This method can be by various objects that are provided within .net framework. One of the mechanism is that

  • Use a connection object and DataAdapter
  • Use a connection object along with Command and use either DataReader or DataSet

These mechanisms are pretty neat and straight, while connecting to the datastore. Where as they don’t provide the query mechanism to the developer against the returned results from the datastore. Ofcourse, you still could do the “Filter” method for the dataset or datatable object. This again doesn’t give the generic way to query from the data in the dataset / datatable.

LINQ, in this context provides a great flexibility for the developer to query against the data schema and provides a decent means of all kinds of DataBase querying techniques. In other words, I could say that LINQ gives the power of Database Querying to the C# developer. If your applications are developed using LINQ, you don’t need to have a DB developer. But you need the DB administrator to understand the health of the database and fine tune the queries used by LINQ. Hence, by choosing LINQ for your projects you are saving cost to the project owner. So, To LINQ or NOT to is upto you to decide. Let me not write more here about  LINQ and jump to the execution mechanisms within LINQ.

As titled in the post, there are 2 mechanisms for the execution of LINQ Results, I say it is about LINQ RESULTS. They are differed execution as well as Immediate execution. The main difference between these two are the lazy loading (or) JustInTime loading at the run time. To make simple this entire story, let me give you some code snippet, so that you would understand. For this am trying to query the Northwind Database.

Requirement:

Display all the employee names along with the product names and total of sold quantity

 

Solution 1 : In this solution am trying to use the LINQ as an Expression.

var empCounts = from eachEmpData in
 (from eachSale in
   (from eachEmp in nwDB.Employees
      join eachOrder in nwDB.Orders on eachEmp.EmployeeID equals eachOrder.EmployeeID
      join eachOrderDetails in nwDB.Order_Details on eachOrder.OrderID equals eachOrderDetails.OrderID
     join eachProduct in nwDB.Products on eachOrderDetails.ProductID equals eachProduct.ProductID
     where eachOrder.OrderDate > dtStart && eachOrder.OrderDate < dtEnd
                          select new
                          {
                              FirstName = eachEmp.FirstName,
                              LastName = eachEmp.LastName,
                              ProductName = eachProduct.ProductName,
                              OrderQuantity = eachOrderDetails.Quantity
                          }
                        )
    group eachSale by new { eachSale.FirstName, eachSale.LastName, eachSale.ProductName } into groupSales
                     select new
                     {
                         groupSales.Key.FirstName,
                         groupSales.Key.LastName,
                         groupSales.Key.ProductName,
                         Sold = groupSales.Sum(qty => qty.OrderQuantity)
                     })
                select eachEmpData;
 
// the below line binds the data from LINQ Expression
bsLINQData.DataSource = empCounts;


In this solution, you could see that I’ve written all the joins similar to that of the Database SQL joins. If you are familiar with all such queries, then it is very easy to write. But this kind of writing is also expensive. The alternative for such queries is the next solution.



Solution 2: In this solution am trying to use the same above LINQ as methods



var empCounts = nwDB.Orders
    .Where(eachOrder => eachOrder.OrderDate > dtStart && eachOrder.OrderDate < dtEnd)
                    .SelectMany(eachOrderDetails => eachOrderDetails.Order_Details)
                    .GroupBy(od => new { od.Product, od.Order.Employee })
                    .Select(od => new
                    {
                        FirstName = od.Key.Employee.FirstName,
                        LastName = od.Key.Employee.LastName,
                        ProductName = od.Key.Product.ProductName,
                        Sold = od.Sum(q => q.Quantity)
                    })
                    .OrderBy(s => s.Sold);

                // the below line binds the data from LINQ Expression
                bsLINQData.DataSource = empCounts;


In this solution, you find less coding and much usage of LINQ as methods. This kind of coding is much better than the previous solution. For the records, the above solution has taken about 3 milliseconds on my laptop where as the current solution took about 1 millisecond to query. See below for the stats and comparison.



Solution 3: The above two solutions are Differed execution, where as the current one is all about Immediate execution.



var empCounts = nwDB.Orders
    .Where(eachOrder => eachOrder.OrderDate > dtStart && eachOrder.OrderDate < dtEnd)
    .SelectMany(eachOrderDetails => eachOrderDetails.Order_Details)
    .GroupBy(od => new { od.Product, od.Order.Employee })
    .Select(od => new
    {
        FirstName = od.Key.Employee.FirstName,
        LastName = od.Key.Employee.LastName,
        ProductName = od.Key.Product.ProductName,
        Sold = od.Sum(q => q.Quantity)
    })
    .OrderBy(s => s.Sold).ToList();

// the below line binds the data from LINQ Expression
bsLINQData.DataSource = empCounts;


Here you have to observe the query ended with .ToList() and that is the trick of this query. When you see the statistics, it is very surprising that the time taken to query is very negligible when compared with binding the query results to the binding source. Which means that it is evident to say, LINQ doesn’t fetch the data when it is queried, but it fetches data when it is actually binded to any source.



Finally, the results when compared the above all the three solutions are as mentioned below



  Query Bind
Expression 3 60
Methods 1 44
InList 35 9


 



Conclusion :



Solution 1 and Solution 2 are the examples for Differed Execution, where as the 3rd solution is an example for Immediate execution. Any comments?

ASP.NET 70-515 Exam Preparation points

I’ve written the mentioned exam and completed this in the second attempt. I’ve failed in my first attempt, but passed in the second attempt. The second attempt for all the Microsoft exams is free. One of my friend asked me about the highlight points that helped me to attend the exam. This post arrived while preparation of the bulleted points that I recollect from the exam.

The whole exam pattern can be divided into 3 + 2 = 5 sections. The first 3 sections are more concentrate on the enhancements in v4.0 along with the traditional questions from v3.0 based. The second 2 sections are mainly into configuration and deployment of any web app that is developed using ASP.NET v4.0

To make the long story short, here is the full details that I’ve in my mind. Ofcourse, I’ve not mentioned any thing about MVC as a separate section, because, MVC is a pattern for me but not a new enhancement from the point of framework. Having mentioned that you can even consider that there are few questions pertaining about MVC too, thus, don’t forget to give attention towards MVC.

1) Traditional WebApplication questions

1) How to Configure Web Forms pages 
2) What are master pages and themes 
3) What is globalization

4) What are the page life cycle events

5) How to implement caching and how to manage view-state & Validate user input

2) Questions that are new to ASP.NET v4.0 Controls

6) What are user controls and how to implement 
7) What are the mechanisms using which we can manipulate user interface controls from code-behind

8) What controls help to Display and Manipulate Data 
9) What are the new ways to implement DataBound / DataSource controls

10) Query and manipulate data by using LINQ (ofcourse, this doesn’t new to v4.0 but the exam questions are based on v3.0 LINQ)

3) This section is about new enhancements from ASP.NET v4.0

11) What is Client-Side Scripting and AJAX 
12) How to induce the dynamic features to a page by using JavaScript 
13) How to handle JavaScript events

3.1)Services Enhancements

14) How to create and consume a data service 
15) How to create and configure a Dynamic Data project

4) Configuration Section targeting the production environment

16) How to configuring and Extending a Web Application 
17) How to configure authentication and authorization 
18) How to configure providers 
19) How to create and configure HttpHandlers and HttpModules 
20) How to Configure initialization and error handling 
21) How to reference and configure ASMX and WCF services 
22) How to Configure projects and solutions, and reference assemblies 
23) What are custom routes and how to create controllers & actions

5) Standard Deployment of WebApps

24) How to debug a Web application 
25) How to deploy a Web application

So, if you are attending this exam, please give your self a second study about the above mentioned points. If you are aware of fundamentals of these topics, that’s it you are thru. Happy certification.

Sending SMS from .NET Application

Today, when visiting the MSDN forums, came across of a request about the subject of this post. While replying, thought of writing a post with visual aids. So that it would be more meaningful. But unfortunate that doesn’t support to upload the Images. Hence is the blog post.

Before we talk about .NET application, it is required to understand how SMS works and the integration with some application. Thus, let’s see what happens when some one sends SMS. Am not going to talk about SMS sent to any phone with 10 digits unique number, but am talking about sending SMS sent to less than 10 digits number, sent on some reserved interfaces (or) mass SMS receiving mechanism. Mostly these kind of requirements come into action when polling is required or a kind of reality shows like in USA and Sa Re Ga Ma Pa from Zee Tv in India.

Let’s start with how this kind of polling is offered by the Providers, called as SCS Providers ie., Short Code Service Providers. Any business person who is interested to utilize the services of such SCS first has to register with these SCSProviders. The SCSProviders charge according to their business plans. Here is a simple example about the pricing by one of the Indian SCS Provider


Short Code Number: 53426
    Short Code: <KEYWORD> As per request *(subject to availability)
    Sub Keyword: Multiple ( e.g. <ShortCode Keyword> <sub-keyword>)
    Control Panel: Web Based
    Auto-Welcome: An SMS will be send to the customer who will send your short code. This message can be customized from control panel. You can create different Welcome message for sub-keywords also.

  Reports: Daily Keyword/Sub Keyword based
    Database: Database of received SMS will be available in your account with us. (Web Based)
    Auto-Forward:To SMS (An SMS will be sent to desired number with Sender’s Mobile Number SMS Forwarding Cost: 25 P/SMS)

  • To Email (An Email will be sent to desired mail id with Sender’s Mobile Number, Keyword)
  • Web URL (This will submit the sender’s mobile number and keyword to desired web-page and the result of the page will used as Reply Back SMS. This web page could be dynamic in nature to give any user specific reply back. e.g. Result of Exam through SMS Request)

  Network Support: All India
    Setup Time: 48 Hrs. after approval of Keyword availability
    Setup Fee: NIL
    Monthly Rental: 4,500/- [4500 + 556 (Service Tax): Rs.5056]
    Quarterly Rental: 4,000/- [12000 + 1483 (Service Tax): Rs.13,483]
    Half Yearly Rental: 3,500/- [21,000 + 2596 (Service Tax): Rs.23,596]
    Yearly Rental: 3,000/- [36,000 + 4450 (Service Tax): Rs.40,450]                                  
 
Rs. 4,450/- Off on Yearly subscription.
 
One Year Pricing (after discount): Rs.36,000/-


Hope you are now clear about the pricing details of any SCS Provider. Let me show you how the SCS provider works with some visual representation. The infrastructure and the working model would be some thing similar to that of the below mentioned

image

Now that you understand how your SMS communication is implemented in real world, let’s see how this can be extended so that any application can handle the situation when SMS received as well as sent.

Most of the SCS Providers do the URL Forwarding, which means they invoke any of the given URL when they get an SMS. Thus, it is the responsibility of the business application to provide any URL to receive the information about the SMS. The URL would be some thing like, http://mybusiness.com/getsms.aspx?phonenumber=93829283928&message=thisiscool

Which means that the business owner should expose some interface by means of a WebPage (or) WebService (or) what ever is feasible for the business. Once the business application gets the information, it is all within the workflow of the rules that is orchestrated by the business owners. The entire crux of this post remains in this last paragraph. Well, for a web developer, I don’t think it is required to explain how to collect the query parameters as well as send any query parameters. This is all fine to collect the SMS sent by the users on a reserved number like 53426. But now, how about sending back some information to the phones via SMS?

As you see in the diagram that every SCSProviders have a feature called as URL Forwarding, they would also be having a web page where you can send some data that would be passed onto the SMS sender. The page that is provided by the SCSProvider would be some thing like http://SCSPWebSite.com/sendsms.aspx?phnumr=93829283928&message=thisisreply  .. Hope now you are totally aware of how to send SMS with the help of SCS Providers as well as receive SMS in a web application.

What do you say?

Visual Basic is the Core attention by Microsoft

Workflow foundation is currently at 4th version. The main coding syntax that is expected for any of the code activities are more from the VB code syntax. Hence, it is proven that Microsoft considers VB as primary language while doing any kind of Research.

Apart of this, there is one more fact that there is a recent toolkit released from Microsoft, just yesterday, ie., 28th Nov 2010. It is RTW for Windows Phone developer. This release enables Visual Basic developers to create Silverlight applications for Windows Phone 7 using the final version of the windows phone developer tools.

You can download the Windows Phone developer tools RTW from this link.

EntLib 5.0 & Unity 2.0 Release Dates

Am a big favorite of EntLib. I started using this from v1.1 during June 2005. The good thing from the latest version is that they are not adding any more blocks, on top of it they are removing some unwanted code.

They are targeting to release this by 14th of April after 2days of VS2010.

RevisedRoadMapv5

Bill Gates started a website

Microsoft’s co-founder, Bill Gates started a new website about his leanings and thoughts that are cooking in his mind. It is really good to know such transparent information from a person like him.

Every January, he writes a news letter to all the employees of Microsoft and shares his ideas to them. This year is some thing different. Why only the Microsofties know about him, not the world. From this thought is raised a new concept of sharing via web.

Did you see this web, the Gates Notes. It has various sections like, Thinking, Learning, Travel, blah.. blah .. Why don’t you go there and start exploring more about him.

BTW don’t forget to convey this to your friends..

Go – an Old wine from Google

Google is coming out with a new programming language with title as “GO” and the tag line for this programming language is, “a systems programming language…”

In my first impression all that got to my mind is that, most of the concepts are taken from programming languages like C and other few more features like GC from current world’s programming languages. The 1Hr talk by Rob is posted on YouTube. Interesting..

After going thru the entire presentation, all it struck to my mind is the title of this post. What about you?

What’s new in .NET

Currently, we see version 4 of .NET Framework is the hot topic. It all started with v1.0 during 2003 and have seen various version changes, such as v1.1, v2.0, v3.0 and v3.5. But what’s new in every version? You might start comparing each version directly with its predecessor.

Did we ever thought interms of the need of the new version? (or) What makes new version much a mandatory for the application to start using them?

Jerry D. Miller, a .Net Developer from Austin, Tx, has a different perspective about the new version of .NET. He says that the new thing about every version is not just the feature set, but the Functional Programming.  Yes, I too felt this. But could not give a shape to this idea. He has written a detailed article about this concept at MSDN. A worth reading article under the tag line of “Patterns in Practice”