Wednesday, 30 December 2009

Options for Building Your Data Access Layer Using .NET

Thinking of building the data access layer of your next application?
What are your options?
What is most important to you; performance, scalability, security, flexibility, rapid development, etc?


  • Use this option if performance is your main contain. No other solution would have higher performance than this as all tools will use plain ADO.NET internally.
  • It's not rapid for development

ADO.NET Entity Framework




Microsoft Enterprise Library Data Access Block



Other references:

Wednesday, 23 December 2009

Download Files from Web Using Wget Tool

Wget is a tool which helps you download web pages.

Download One Page:


Monday, 14 December 2009

JavaScript - Naming Conventions and Coding Standards

Naming Conventions:

I am going to write naming conventions for JavaScript. This will be inspired by ASP.NET AJAX Library.

Target Convention Example

js file name

Pascal Casing



Pascal Casing



Pascal Casing



Camel Casing


Input Parameters

Camel Casing

parse(text); // text is in camel case

Private Class Variables

use _ with camel casing

var _firstName;

Public Class Variables

this.<Camel Casing>

this.isStrong = true; // this is used to define a member as public

Class Properties

// properties are public

get_<camel casing>


set_<camel casing> 

this.get_firstName: function(){
return _firstName;

this.set_firstName: function(value){
this._firstName = value;
Method Variables Camel Casing var index = 0;

Coding Standards:

Friday, 11 December 2009

All About WatiN

Get a RadioButton


Click a Button:


Click a LinkButton:

Type in a TextBox:

Wednesday, 9 December 2009

Approaches to Unit Testing JavaScript

1. Create your JavaScript functions

2. Create and Host Tests

Create an HTML file to host your tests using QUnit

3. Provide Test Runner

You need a test runner which can be integrated with CruiseControl.NET and Visual Studio.NET:

Tuesday, 8 December 2009

JavaScript – Create Singleton… No… Static Class

The following class is a normal javascript class:

function Calculator() {

// public functions
this.Add = add;
this.Subtract = subtract;

// private functions
function add(a, b)
{ return a + b; }

function subtract(a, b)
{ return a - b; }

// private variable
var name = "pooya";

// public variable
this.Name = name;


And this is how you use it:

var calculatorObject = new Calculator();
calculatorObject.Add(1, 2);

The following class is a static version of the same class:

var Calculator = (function() {

// private variable
var name = "pooya";

// private static functions
function add(a, b)
{ return a + b; }

// all the public members should be added to return (comma separated)
return {
// public function using delegate
Add: add
// public function embedding the implementation
Subtract: function(a, b)
{ return a - b; }
// public variable
Name: name

Now you can simply use the following:

Calculator.Add(1, 2);


I have seen many people saying this is how you create a Singleton class which is incorrect.

Please note that in the second Calculator class, no instance member can be defined which means "this" doesn't work, the following is invalid: "this.Name = name"

All members are considered static because of the way it has been defined.

The difference is that in Singleton class you can define instance members, however, in static class like this one you can’t.

You can argue that Singleton in C# is different from Singleton in JavaScript but in my view Singleton is Singleton no matter what language, so this is how you define a static class in JavaScript.

Monday, 7 December 2009

Networking - What is SAN?

What is SAN?
A storage area network (SAN) is a high-performance subnet (or shared storage bus) whose primary purpose is the transfer of data between computer systems and storage elements.

Benefits / Motivation?
  • Availability
  • Reliability
  • Scalability
  • Performance
  • Manageability

How to implement?


Friday, 4 December 2009

How to Use JSON in JavaScript and .NET?

It's very easy to use JSON, believe me!

JSON is a lightway format of sending messages from one place to another.

From JavaScript:

Use JavaScriptSerializer class to serialize into JSON and deserialize from JSON:

To use this you should add ScriptManager on your aspx page.

Examples: ...

From .NET 3.5 and above:

1) Use JavaScriptSerializer class:

This class is located in System.Web.Extensions.dll.

2) Use DataContractJsonSerializer class:

This class in located in System.ServiceModel.Web.dll.

DataContractJsonSerializer Example:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;

namespace CompanyName.ApplicationName.Model
public class CMMessage
public string ClientIndividual;

public string JobFunction;

public string LastTeamActivity;

public string ClientCompany;

public List CMPhoneList;

/// This method is responsible to json serialize this object
public string ToJson()
var serializer = new DataContractJsonSerializer(typeof (CMMessage));
using (var memoryStream = new MemoryStream())
serializer.WriteObject(memoryStream, this);
return Encoding.Default.GetString(memoryStream.ToArray());

/// This method is responsible to deserialize a json text to a CMMessage object
/// json text
public static CMMessage GetByJson(string json)
var serializer = new DataContractJsonSerializer(typeof(CMMessage));
using (var memoryStream = new MemoryStream(Encoding.Default.GetBytes(json)))
return serializer.ReadObject(memoryStream) as CMMessage;


From .NET 2.0:

I am afraid Microsoft didn't create any class for JSON Serialization in .NET 2.0, however, there are some workarounds for you:

1) Use

2) Create your own JSONSerializer for .NET 2.0 using .NET Reflector on either JavaScriptSerializer class or DataCotnractJsonSerializer class in .NET 3.5:

More resources:

Tuesday, 1 December 2009

JavaScript - ReplaceAll Function

JavaScript ReplaceAll Function:
String.prototype.ReplaceAll = function(stringToFind, stringToReplace) {

if (stringToFind == null stringToFind == '') return this;

var result = "";
var temp = this;

var index = temp.indexOf(stringToFind);

if (index == -1) return this;

do {
var targetLength = index + stringToReplace.length;
temp = temp.replace(stringToFind, stringToReplace);
result += temp.substring(0, targetLength);
temp = temp.substring(targetLength, temp.length);
index = temp.indexOf(stringToFind);
} while (index != -1);

// to add any remaining data
result += temp;

return result;


How to use it:

function Test() {

var data = "Hi, my name is Pooya Khamooshi; I am expert in everything (potentially)!";
var replaceMe = 'Pooya';
var replaceWith = "<b>" + replaceMe + "<\b>";

if (data.indexOf(str) == -1)
document.writeln("no match found");
document.write(data.ReplaceAll(replaceMe, replaceWith));

Use regular expression

This approach is easier and shorter but it requires that you know exactly what your regular expression is.

var str = "My name is Pooya Khamooshi and I have understood that there are many things I am not expert in";
var pattern = /\s/g;
str.replace(pattern, ''); // this removes all the spaces from the text above.