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?

Plain ADO.NET

Pros:
  • 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.
Cons:
  • It's not rapid for development

ADO.NET Entity Framework

Pros:

Cons:

More:

Microsoft Enterprise Library Data Access Block

Pros:

Cons:

More:
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:

wget http://fly.srk.fer.hr/





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

MicrosoftAjax.js

Namespace

Pascal Casing

Sys.Serialization;
Type.registerNameSpace(“Model”);

Class

Pascal Casing

JavaScriptSerializer

Methods

Camel Casing

serialize(text);
deserialize(object);

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>

or

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

ie.RadioButton(Find.ByName("rdoSendInfo_0")

Click a Button:

ie.Button(Find.ById("btnInitialize")).Click();

Click a LinkButton:
ie.Link(Find.ById("LinkButton1")).Click();

Type in a TextBox:
ie.TextField(Find.ByName("q")).TypeText("WatiN");



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
http://www.bitmechanic.com/blog/?p=20

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);

Notes:

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
Costs?

How to implement?

Links:

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
{
[Serializable]
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 http://json.codeplex.com/

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

http://geekswithblogs.net/Mochalogic/articles/103330.aspx

More resources:
  • http://msdn.microsoft.com/en-us/library/bb299886.aspx

Tuesday, 1 December 2009

JavaScript - ReplaceAll Function

Approach1:
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");
else
document.write(data.ReplaceAll(replaceMe, replaceWith));
}

Approach2:
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.