Tagged: .Net

StackOverflow featured as Microsoft Case Study

I bumped into this Microsoft Case Study showcasing Stack Overflow site and its ASP.Net MVC based architecture. [link]. The point of interest for me was the infrastructure specs for that site…

# Presentation. Customers browse pages served by Windows Server 2008 Standard, through IIS 7.5. The Web servers are computers with Xeon quad core CPUs and 8 gigabytes (GB) RAM. When a user submits a question or an answer, their information is written to the database using the lightweight LINQ to SQL database abstraction layer.
# Database. The database is hosted on a dedicated server with 2 Intel Xeon quad core CPUs and 48 GB of RAM, and runs Microsoft SQL Server 2008 Enterprise. Stack Overflow takes advantage of the Online Indexing feature, introduced with SQL Server 2008 Enterprise, which enables table re-indexing while the database continues to run.

Look at the number of horses that run that SQL Server backend… 48GB of RAM !!!!!!

Useful Lint Programs for all Web UI developers

All Web UI developers have to deal with JavaScript and lots of it. And these days with many of us are using
JSON as a data exchange format, we are looking into massive JSON strings on a daily basis when developing/debugging. There are 2 Lint programs, JSONLint and JSLint that can help in making life easy for developers. Lint programs does a static code analysis and comes up with errors and warnings. It also beautifies your code (if it not already intended, formatte, etc) for better readability. This is very useful when you are grabbing this code block from a debug window or from firebug console and trying to make sense
of it.

Try it today 🙂
— JSON Lint here.
— JSLint here.

JSLint is a JavaScript “code quality” too. This means it does more than “Linting”, it actually inspects your
JavaScript code for quality points set by great Doug Crockford (creator of JSON). These can be configured thru many options that are available. Check out the options here.

While we are on this topic, another great tool is the JS Beautifier. I have found this of immense value when you are working with a “packed” or “minified” version of JavaScript (works only with code packed using Dean Edwards Packer). Plug it into the beautifier and voila, you have formatted and readable JavaScript.

Happy programming!


VB.Net and C# Co-Evolution

Read Scott Wiltamuth’s blog about future of VB.Net…Its not going away anytime soon, in fact MS is planning
to have a co-evolution strategy for C# and VB.net (and other .Net languages). I think this is effort should be
welcomed, because somewhere after v1.1 of .net, language features introduced have very different usage syntax in VB.Net and C# ! This would mean that it is more and more difficult for a good .Net programmer to code in VB.Net and C# with the same ease.
Have you noticed that if you search for a solution in web, you always get tons of C# samples but not many VB.Net samples..its a fact. So what does a VB.Net developer do? He takes C# code and converts to VB.Net, so if there is a lot of syntactical differences, his life is a mess, if not “all is well”


Which JSON Serializer to use?

This is an easy one, but just want to make a note for easy reference. JSON is more ubiquitous than ever these days in ASP.Net web development. From AJAX enables web sites to MVC AJAX, JSON is “the” data-interchange format we want to use.

There are 2 possible class libraries that you can use in ASP.net to serialize objects to JSON and back. If you are using WCF and your custom objects are marked as [Data Contratcs], then DataContractJsonSerializer (System.Runtime.Serialization.Json) is the class you want to use. But if you are using an object that you cannot mark as a DataContract (may be part of a third-party component), then you want to use JavaScriptSerializer (System.Web.Script.Serialization).

Here is some code snippets that shows both approaches:

/* Using JavaScriptSerializer */
String s;
JavaScriptSerializer ser = new JavaScriptSerializer();
ser.MaxJsonLength = Int32.MaxValue;
s = ser.Serialize(obj);

JavaScriptSerializer ser = new JavaScriptSerializer();
ser.MaxJsonLength = Int32.MaxValue;
obj = ser.Deserialize(json);
return (obj);

/* Using DataContractJsonSerializer */

DataContractJsonSerializer ser = new DataContractJsonSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, obj);
string retVal = Encoding.Default.GetString(ms.ToArray());

T obj = Activator.CreateInstance();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
DataContractJsonSerializer ser = new DataContractJsonSerializer(obj.GetType());
obj = (T)ser.ReadObject(ms);

Happy programming!

A LINQ Thought

LINQ stands for Language Integrated Query. I want to stress on “Integrated” because I think the strength of LINQ comes from the fact that it is a data query engine that is integrated with .Net languages. This very reason gives us many features that we as developers are comfortable
using in a language into our data manipulation operations.

Lets take a simple example,

var query
= from customer in CustomerTable.AsEnumerable()
where customer.Field("Name").ToLower().Contains("John")
select customer;

In the above example, we are using simple string functions that are part of C# string class to do our comparisons for our data fetch.
Lets take another example, where we will use a C# function to evaluate a sub-query/expression,

var query
= from customer in CustomerTable.AsEnumerable()
select new Customer 
Name = customer.Field("Name"),
State = customer.Field("State"),
CustomerType = GetCustomerType(customer.Field("State"))

In the above example, GetCustomerType() function is used inline with a LINQ query to evaluate some business rule.
In short, with great flexibility like this, LINQ has made life of developers much easier!


GridView Sorting Trick when using Object Datasource with custom objects

When you are using Object DataSource in ASP.Net for GridView, if your select method is returning an object that is of type DataView, DataTable or DataSet, you will be able to get sorting automatically.
But if you are like me and your architecture has a distinct DTO (Entity) layer, and you pass back and forth custom objects or generic list of these custom objects, then you are in less luck with GridView. If you
are binding GridView with Object Datasource that returns a List, you will need to implement your own custom code to do sorting.

If you google this issue, you will find different implementation that will point you to add a OnSorting event handler and more than one way to implement a custom sorting.

What my thinking is, since DataTable and DataView provides us the automatic sorting that we need, then why don’t we create
a DataTable from the List we have. We can do this elegantly using generics and reflection. Once we create a DataTable, simply pass back a DataView based on this table as return parameter from your Select Method of Object Datasource.

Here is the code for converting a List to a DataTable, (CustomObject is my custom object defined in my DTO layer)

using System;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
public class Utilities

	public static DataTable ToDataTable(List lst) where T : CustomObject
            PropertyInfo[] propertyInfos;

            System.Data.DataTable table = new DataTable("GridViewTable");
            DataColumn column=null;
            DataRow row=null;

            foreach (CustomObject v in lst)
                propertyInfos = v.GetType().GetProperties();
                foreach (PropertyInfo propertyInfo in propertyInfos)
                    column = new DataColumn();
                    column.DataType = System.Type.GetType(propertyInfo.PropertyType.FullName);
                    column.ColumnName = propertyInfo.Name;
                //New table made, break the loop
            //Now populate the table with values
            foreach (CustomObject v in lst)
                 propertyInfos = v.GetType().GetProperties();
                 row = table.NewRow();
                 foreach (PropertyInfo propertyInfo in propertyInfos)
                     row[propertyInfo.Name] = propertyInfo.GetValue(v, null);
            return (table);