Posts

Executing synchronous methods asynchronously

We can achieve certain advantages by writing asynchronous methods. It's pretty simple to write them in C# code: we just need to use  async  and  await keywords properly. Grammar of these keywords can be found in detail in MSDN . In this post I will try to focus on writing few code to invoke a regular synchronous method asynchronously. Let's assume a scenario where we need to perform an expensive database operation when a particular information is changed from the front end. For example: I have an application to manage employee information and I've changed the basic salary for a particular grade using my application. Now I need to update salary of all the employees under that grade. The later operation will be slower one and I don't want this to block me from doing other stuffs. Here is a piece of synchronous code to update salary information: public   bool   UpdateEmployeeSalary ( string   grade ) {      try  ...

Different approaches for paging SQL data

Image
Pagination is a common technique to improve user experience while pulling large chunk of data from SQL. But even pulling paged data could become an overhead while  retrieving  data across multiple tables. Here I will go through some of the techniques for pagination. For illustration I am assuming there is an "Activities" table that holds logs for all activities throughout an application. I will be retriving paged data from this table. Approach 1: using ROW_NUMBER This is the most common approach for getting paged data from SQL query and it works pretty well for simple queries. Here is an example: DECLARE   @StartIndex   INT ,   @EndIndex   INT ; SET   @StartIndex   =   51 SET   @EndIndex   =   100   SELECT   *   FROM   (   SELECT   Count ( 1 )   OVER  ()   AS   TotalCount ,          ROW_NUMBER ()   OVER ...

Creating MVC RESTful API with Backbone.js - Part 3

This is the last part of the series for creating MVC RESTful API with Backbone.js. In previous two posts I have: Created MVC RESTful API Created Backbone model, collection and views In this part, I will be rendering the client-side backbone views on server-side razor view pages. I have two razor views: Index.cshtml and Details.cshtml . In Index.cshtml I will be rendering the  ProductListView (backbone view) that I created before. Here is how to render this: Here at the very beginning I am initializing the Products collection and then fetching collection data. The fetch method will cause to send a GET request to the specified URL of the collection. I am initializing and rendering ProductListView inside success callback of collection fetch method so that the view is rendered after the collection data is ready. The "el" attribute of the view indicates the root DOM element of this view. Similarly ProductView is rendered at Details.cshtml : Here I...