Pages

Sunday, May 17, 2009

Exclude Pages from ASP.NET Authentication


Recently i discovered an interesting feature of asp.net where we can exclude page(s) from asp.net authentication. Usually once we have asp.net authentication setup, our requests to different pages must be authenticated. But at times we may require to exclude some of the page(s) from this restriction. This can be easily done by adding a <location> section in the web.config file. The <location> tag goes under the <configuration> section and can be defined as following:


<location path="excludepage.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>


This tag can also be applied to page in a specific folder as following:

<location path="myfolder/excludepage.aspx">

We can have several <location> tags defined for seperate pages in a web.config file. However, if you have many pages to be excluded, a better approach is to move all the pages under one folder and specify this folder in the <location> tag as following:

<location path="myfolder">

Do keep in mind that if excluded pages reference a css, image etc file, those too must be excluded in order work properly.

Hope this post has been helpful. Stay tuned for more...

Wednesday, May 13, 2009

Crystal Report with Alternate Row Color


In my previous post, I walked you through creating a simple crystal report in visual studio. I also mentioned that in the following posts, I will touch upon different topics relating to crystal report. Creating a crystal report is easy; however the users always expect better looking reports for their use.

One of the questions which pops up on many forums is ‘how to have alternate row color’ in a crystal report just like a gridview in asp.net. To have alternate row color in a crystal report, you have to format a section. A section is separated by a gray bar on the report. There are different sections in a report including ReportHeader, PageHeader, GroupHeader, Details and ReportFooter sections. Usually records appear under the Details Section. To have alternate row color, follow these steps:


1. Right click on Details Section and select Section Expert…



2. In the Section Exert panel, select the Color Tab and click on the formula icon (with x+2 written on it).



3. In the Formula Section panel, we have to write code to change the color for alternate rows.



4. Enter the following code as in listing 1 and click on Save and close icon at the top.

Listing 1


IF RecordNumber mod 2 = 0 Then
crRed
Else
NoColor


Now when you view the report, you will see alternate rows with a red background. That is all required to have the required alternate coloring effect. Hope this post was useful for you. Stay tuned for more…

Creating Crystal Report with Visual Studio


Introduction

Crystal Reports is a power reporting tool which ships with visual studio. It greatly enhances the process of creating reports. Crystal reports provide a Rapid Application Development style reporting model for both web and windows applications. Although creating reports with visual studio is a breeze, however; it takes some time and efforts to get acquainted with many of crystal report features. In this and following posts on crystal reports, we will have a look at these concepts to get a handle on crystal reports.

Crystal Reports integrate seamlessly with Visual Studio. Crystal reports works with ADO.NET, web services, flat files, MS Access and XML. We can easily export data from crystal reports to different formats including PDF, MS Word & Excel and Rich Text Format. The Crystal Reports API provides a rich programming model for creating and customizing reports. Before we dive into creating our first reports, following are some of concepts you should understand.


Data Access Model

Crystal Reports can either use a Push or a Pull model to access data. In a pull model, the crystal report engine connects to the data source through a driver, executes SQL Commands against the data source and publishes the data on the report.

In a push model, the task of talking to the data source is left with the developer. The developer writes the code to hold the data in a data container such as a dataset, datatable or a custom collection and then later assigns it to the report.



Use Page_Init Method

On many of the forums, a problem commonly observed is that a report does not display properly when any of the button on the report toolbar (every report has a report toolbar at the top with different functionality) is clicked. Why this happens is due to a simple reason – Don’t use the Page_Load method. The data which binds and provides login information to a crystal report should always reside in the Page_Init method.

Creating Crystal Report using Visual Studio

Creating a crystal report with visual studio is straight forward. In this section, I will walk you through creating a crystal report for a web application. I will use a step-by-step approach for the readers. So lets get started:

1. Create a new a new web site in Visual Studio.
2. In the solution explorer, right click and click on Add New Item.
3. In the Add New Item Template, select Crystal Report. In the Name field, type NorthwindProductReport.rpt. Click Add.


Fig 1

4. A new dialog box Crystal Reports Gallery appears. Select the Using the Report Wizard option under the Create New Crystal Report Document panel. Also select the Standard option under the Choose an Expert panel and click Ok.



Fig 2

5. The Standard Report Creation Wizard dialog box appears. Under the Available Data Sources panel, expand the nodes Create New Connection and OLE DB (ADO).



Fig 3

6. A new OLE DB (ADO) dialog appears. Under the Provider panel, select Microsoft OLE DB Provider for SQL Server. Click Next.



Fig 4

7. Under the Connection Information panel, enter in the Server, User ID, Password fields. Select Northwind Database. Click Finish.



Fig 5

8. You return to the Standard Report Creation Wizard dialog. The OLE DB (ADO) has a new connection to the Northwind database. Expand the Northwind, dbo, Tables and select Products. Click the > symbol to move the Products table to the Selected Tables panel. Click Next.



Fig 6

9. Under Fields and Available Fields panels, expand the node Products. Select ProductID, ProductName, QuantityPerUnit and Unit Price by holding the Ctrl Key. Click the > symbol to move these fields to the Fields to Display panel. Click Next.



Fig 7


10. Under Grouping and Available Fields panels select the Products.ProductID field and click the > symbol to move the field to the Group By panel (Note: The Grouping function is similar to Group By clause in SQL Server. If there are multiple records for a grouping field, they are grouped together e.g. we can group Orders by Customers in a report). Click Finish.



Fig 8

11. The report is created with four fields on it. You can change its properties by right clicking on it and selecting Format Object. You an also add different objects on this report including a line, box and custom text to layout the report.



Fig 9

12. At the bottom of the report, you will find two tabs Main Report and Main Report Preview. Click on Main Report Preview to preview the report.



Fig 10

13. Now that we have created that report, we can easily embed it in a web page using minimal code. For this purpose, right click on solution explorer and click on Add New Item. In the Templates view, select Web Form and type Products in the Name field. Click Add.



Fig 11


14. On the web form, drag an instance of CrystalReportViewer from the Toolbox -> Reporting.



Fig 12

15. In Code View, declare Page_Init method as in listing 1:


Listing 1

protected void Page_Init(object sender, EventArgs e)
{
BuildReport ();
}


Next code the private BuildReport method described in listing 2 used in the above code:

Listing 2

private void BuildReport ()
{
ConnectionInfo connInfo = new ConnectionInfo ();

connInfo.ServerName = "Personal";
connInfo.DatabaseName = "Northwind";
connInfo.UserID = "reportuser";
connInfo.Password = "secretlogin";

CrystalReportViewer1.ReportSource = Server.MapPath ("NorthwindProductReport.rpt");
ReportLogon (connInfo);
ReportLogon (connInfo);
}


Finally, the above code calls the method ReportLogon described in listing 3:

Listing 3

private void ReportLogon (ConnectionInfo connectionInfo)
{
TableLogOnInfos tableLogOnInfos = CrystalReportViewer1.LogOnInfo;

foreach (TableLogOnInfo tableLogOnInfo in tableLogOnInfos)
{
tableLogOnInfo.ConnectionInfo = connectionInfo;
}
}



16. Now if you run the application, you will see a report displayed as following:



Fig 13


Summary

In this post, I walked you through creating a simple crystal report. This is just the tip of the iceberg. There is so much more to be done with crystal reports. In my following posts, I will touch on different topics relating to crystal reports. Some of them will be walkthroughs while others will be tips to help you write better crystal reports. So stay tuned for more…

Monday, March 23, 2009

Reading a CSV File in ASP.NET



Recently I came across a programming need of reading a CSV (Comma Separated Values) file in ASP.NET. Thanks to ADO.NET, reading a CSV file in ASP.NET is a breeze. I am writing this post to share my understanding with you.

Reading a CSV File

A comma-separated-value (CSV) file is commonly used for daily programming needs. CSV files are used to import/export data from databases and spreadsheets. These are simple text file (*.txt, *.csv, *.dat) where data values are separated by a comma or some other character. Usually, the first row has the column-names followed by rows of data. Each row has the data in columns separated by some character. Following is a sample (Northing database – Customers table) of what data looks like in a CSV file with the first row having column-names:

CustomerID, CompanyName, ContactName, ContactTitle
ALFKI, Alfreds Futterkiste, Maria Anders, Sales Representative
ANATR, Ana Trujillo Emparedados y helados, Ana Trujillo, Owner

In ASP.NET, we can easily read and get data from a CSV file using ADO.NET. Listing 1 illustrates the code for reading a CSV file:

Listing 1



// filePath: Path where file resides
// filename: File name to be read
private DataTable ReadCSVFile (string filePath, string fileName)
{
string connString;
OleDbConnection conn;
OleDbDataAdapter da;
OleDbCommand cmd;
DataTable dt;

connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath +
";Extended Properties='text;HDR=Yes;FMT=Delimited'";

conn = new OleDbConnection (connString);
conn.Open ();

cmd = new OleDbCommand ("SELECT * FROM " + fileName, conn);
da = new OleDbDataAdapter (cmd);
dt = new DataTable ();

da.Fill (dt);

return dt;
}


The above code is straight forward with a few points:

1. In the connection string, HDR stands for header indicating that the first row has the column-names.
2. Similarly, FMT stands for format and specifies the formatting type. By default it is set to Delimited which specifies a comma-delimited value. The other values include Delimited (x) where x is the specific character, TabDelimited which specifies tab-delimited values and FixeLength which specifies fields with fixed length.
3. The filePath specifies the file path on the machine hosting the application. If the application is hosted on the server then filePath reflects path on the server.

For your interest I would like to mention that we can also make a join between different CSV files. For example, we can use the following query to join two files.

“SELECT * FROM fileName1 f1, fileName2 f2 WHERE f1.CustomerID = f2.CustID”;

Hope this post has been useful for you. Stay tuned for more…

Saturday, March 21, 2009

LINQ Explained – Part 3



This is the third part of my on-going series on LINQ. In the second part, we looked at some of the underlying C# features which are important to understand to work with LINQ. In this part, we will conclude with the rest of the features. So let us dive in straight.


Yield Statement

The Yield statement was introduced with C# 2.0. In my previous post, we talked about Enumerators. Enumerators help us iterate through collections and custom classes. Collections and custom classes must implement the IEnumerable interface. This interface has one method, GetEnumerator, which returns an instance of IEnumerator interface. The IEnemrator interface performs the actual iteration.

All this is pretty straight forward but does require some coding on behalf of the developers. Implementing IEnumerator interface for complex classes can be time consuming. Wouldn’t it be nice if the compiler could handle the enumeration process for us? Thanks to the yield statement, this is still possible.

Let me explain this concept by the help of the following simple example:

Listing 1


protected void Button1_Click (object sender, EventArgs e)
{
foreach (string fruit in BuyFruits ()) // ref 1
{
ListBox1.Items.Add (fruit);
}
}


private IEnumerable BuyFruits ()
{
string [] fruits = new string [] {"apple", "orange", "coconut", "papaya",
"mango"};

for (int i = 0; i <= fruits.Length - 1; i++)
{
yield return fruits [i]; // ref 2
}
}


If you have noticed, BuyFruits method has a return type of IEnumerable (reference type) but it returns a string (value type) using the yield return statement. Although BuyFruits is a method, it is acting as a class which performs iteration. This may look strange but under the hood, two things are happening.

First, the yield statement, a compiler directive, instructs the compiler to generate an inner (nested) class which implements the IEnumerator interface. It is this inner class which handles iteration for us. Fig 1 shows this class which has been generated using the ILDASM tool. The nested class is named as d__0 and implements the generic and non-generic versions of IEnumerable interface. It also implements the member functions including MoveNext, Reset method and Current property.

Fig 1



Second, the yield statement returns a single element at a time but maintains state between calls. This means that each subsequent call to yield return statement will return the next element in the collection. This is possible because the compiler maintains a state engine which resumes execution from the previously returned value.

The first time the yield statement is executed in the loop, a new object of the inner (mentioned above) class is created. This instance is used across the loop until it iterates through and reaches the end of the entire collection. Each yield return is delegated to the MoveNext method of the inner class. After the loop terminates, the instance of the inner class is also disposed. A new instance is created for each new call. This makes it type-safe across calls.

The code in Listing 1 works under the above principles. First, a class implementing the IEnumerator interface is generated (Fig 1). Next in the Button1_Click event, the foreach loop calls the BuyFruits method. The first call will create an instance of the generated class. Each yield return call is then delegated to the MoveNext method. This way it iterates through the entire collection.

We can also implement enumeration for a custom collection using the yield statement. Following is the code for the custom collection:

Listing 2


public class FruitCollection : IEnumerable
{
private string[] fruits;

public FruitCollection ()
{
fruits = new string[] {"banana", "apple", "mango", "apricot",
"kiwi"};
}

public IEnumerator GetEnumerator ()
{
foreach (string fruit in fruits)
{
yield return fruit;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
FruitCollection basket = new FruitCollection ();

foreach (string fruit in basket)
{
ListBox1.Items.Add (fruit);
}
}


The FruitCollection class implements the IEnumerable interface so GetEnumerator method must be implemented. Within this method, a list of strings is iterated using the yield statement (Note: We can use any looping technique). As mentioned above, a private nested class is generated. This class performs enumeration on behalf of FruitCollection for each yield return statement. The rest of the process is the same as mentioned above. We can then iterate throught the collection as shown in the Button1_Click event.

The yield break statement is another dialect of the yield statement. This statement can stop iteration at any point in the loop. The following snippet will only return the first string in the array.

Listing 3


private IEnumerable BuyFruits()
{
string[] fruits = new string[] {"apple", "orange", "coconut", "papaya",
"mango"};

for (int i = 0; i <= fruits.Length - 1; i++)
{
if (i > 1)
yield break;

yield return fruits[i];
}
}


One last point to mention is that yield statement is equally applicable to generic and non-generic types. For generic types, the IEnumerable interface is used.


Extension Methods

Extension methods are one of the new features added to C# 3.0. According to MSDN “Extension method enable you to ‘add’ methods to existing types without creating a new derived type, recompiling or otherwise modifying the original type”. As the definition implies, we can add new functionality to existing types, primitive or custom.

Extension methods are a special breed of static methods which are invoked like regular instance methods. Extension methods can be added to existing primitive types, classes, structures and interfaces. These methods are static and are defined in a separate static class. Importantly, the first parameter in an extension method defines the type the method will operate on. This parameter must be preceded by this keyword. For example an extension method with first parameter as this string input is available for string data types and input represents the string which invoked the extension method.

Let us see a simple example of an extension method. The following method adds a greeting message to a string type:

Listing 4


public static class GreetingsClass
{
public static string AddGreetings (this string name) //note the input parameter
{
return String.Concat ("We welcome you ", name);
}
}


We can now invoke the AddGreetings method with the following code:


protected void Button1_Click(object sender, EventArgs e)
{
string emp = "Employee";

txtMessage.Text = emp.AddGreetings ();//invoked as a regular method
}


The extension method AddGreetings is defined in a separate static class. The first parameter of the method preceded by this keyword makes it an extension method. Using Intellisence, if we look at the list of available methods for the string ‘emp’, we can locate AddGreetings method along other methods.

Extension methods can also be added to existing .NET classes. In listing 5, an extension method is added to the built-in Stack class. The extension method finds all items in an integer stack which have a value greater than the input parameter.

Listing 5


public static class StackExtender
{
public static int ItemsGreaterThanInput (this Stack stack, int maxValue)
{
int count = 0;

foreach (object o in stack)
{
if (Convert.ToInt32(o) > maxValue)
count++;
}

return count;
}
}

protected void Button1_Click(object sender, EventArgs e)
{
Stack intStack = new Stack ();

intStack.Push(5);
intStack.Push(4);
intStack.Push(1);
intStack.Push(8);

TextBox1.Text = "Total integers greater than (2) = " + intStack.ItemsGreaterThanInput(2).ToString();
}


The above code is pretty straight forward but it has two parameters. The first parameter is the type (Stack) on which it operates. The second is a required parameter which should be provided when this method is invoked.


Var keyword

C# 3.0 introduced another useful feature, the var keyword which allows us to declare implicitly typed variables. An explicit type declaration of these variables is not required which is handled by the compiler. The following line declares a variable of type string which is inferred by the compiler:

var greet = “Hello World”; // no use of string type

It is important to note that these variable types are strongly typed. A proof of this is that the type of these variables cannot be changed later on. For example, the above statement will result in an error “Cannot implicitly convert type 'int' to 'string' if later used as:

greet = 10;

The var keyword can only be used with local variables. An attempt to use it with class level variables will result in an error ”The contextual keyword 'var' may only appear within a local variable declaration”. Since the compiler infers the type of these variables so they ‘must be’ initialized when declared.

The var keyword also rids us of extra type declaration. For example, the following code:

ArrayList alst = new ArrayList ();

can also be written as:

var alst = new ArrayList ();

Similarly, the value returned from a method can also be assigned to a ‘var’ variable. For example, the following method returns an ArrayList:



private ArrayList GetMyList()
{
ArrayList alst = new ArrayList ();
// use alst;
return alst;
}


This method can be directly assigned to the ‘var’ variable:

var myList = GetList ();

If we change the implementation of the above method to return an instance of IList, the compiler is still smart enough to infer the type.



Anonymous Types

Anonymous types are a new feature added to C# 3.0. The idea is to create a type on the fly without actually declaring it. The compiler infers the type at runtime. This gives us a lot of flexibility in defining new types without actually declaring them.

How a type is created ‘just like that’ is based on the concept of object initialization. Object initialization was also introduced with C# 3.0. The idea is to initialize properties when creating objects without invoking the constructor. Let us consider the class in listing 6 (for those who are new, we can now use ‘automatic properties’ where a variable per property is not required):

Listing 6


public class Fruit
{
// automatic properties
public string FruitName { get; set; }
public int FruitPrice { get; set; }
}



The above class can be instantiated as following:

Fruit fruit = new Fruit ();
fruit.FruitName = “apple”;
fruit.FruitPrice = 10;

The above snippet is straightforward but can be time consuming for large classes. Using object initialization, we can reduce the amount of work required to initialize a class with the following syntax:

Fruit fruit = new Fruit {FruitName = “apple”, FruitPrice = 10 };

The concept can be further extended to generic types as following:


List<Fruit> basket = new List<Fruit>{
new Fruit {FruitName = “apple”, FruitPrice = 10 },
new Fruit {FruitName = “mango”, FruitPrice = 15}
};


Coming back to anonymous types, these are created at runtime. Creating an anonymous type is facilitated by ‘object initialization’ and ‘var’ keyword. The following snippet creates an anonymous type - Employee:

var Employee = new { Name = “Emp1”, Age = 35, Salary = 3000 };

We can even create a nested anonymous type using the same syntax. For example, the above code can be extended to add a nested type Address as:

var Employee = new { Name = “Emp1”, Age = 35, Salary = 3000,
Address = new { HouseNo = “H1”, Block = 3, City = “MyCity” }
};

Anonymous methods are extensively used in LINQ. The following post will describe this concept further.



Lambda Expressions and Anonymous Methods

Lambda Expressions are a new feature added to C# 3.0. They provide a handy way to write concise code. But before we jump on to this topic, it is important to understand anonymous methods.

C# 2.0 introduced the concept of ‘anonymous methods’. As the name implies, these are name-less methods which can be declared inline. The method is declared and implemented at the same place. The idea is to avoid defining a separate method which is not reused.

An anonymous method can be used implicitly at a place where a delegate is expected. As we know, when a delegate is called, the referenced method is invoked. In case of an anonymous method, the delegate is replaced by piece of code - inline. The anonymous method is defined using the delegate keyword followed by an optional list of parameters (within parenthesis) and the body of the method. If an anonymous method doesn’t have any parameters, we can omit the parameter parenthesis. An expression defining an anonymous method is known as anonymous-method-expression and has following syntax:

delegate (optional_signature) { // method body - code }

It is important to mention that the signature of the anonymous method must match with the delegate signature (this is how delegates basically work) being replaced. The return type and input parameter list must be the same.

Let us see a simple example of using anonymous methods. Delegates play a de-facto role in events-based programming. Consider the following code for an event:



protected void Page_Load (object sender, EventArgs e)
{
Button1.Click += new EventHandler (Button1_Click);
}

protected void Button1_Click (object sender, EventArgs e)
{
// implementation
}


Using anonymous method we can re-write the above as following:

Listing 7


protected void Page_Load (object sender, EventArgs e)
{
Button1.Click += delegate (object sender1, EventArgs s) { /* implementation */ };
}


In the code above, the EventHandler delegate has been replaced by an anonymous method. If we use ILSADM tool to view the assembly, you will find a new private method with matching signature as illustrated in figure 2. Under the hood, it is this method (b__0) which is invoked by the compiler:


Fig 2



An anonymous method can be declared within a static or an instance method. This also determines the type of the anonymous method, though private. Let us see another example where we actually declare a delegate. This anonymous method will perform some (complex :-) computation:

Listing 8


public delegate int MathDelegate (int n1, int n2, int n3); // declare delegate

protected void Button1_Click (object sender, EventArgs e)
{
int result;

MathDelegate mathdel = delegate (int num1, int num2, int num3) /* anonymous type */
{
return (((num1 * num2) / num3) + 50); // any calculation
};

result = (int) mathdel (10, 10, 2); // invoke delegate and cast reture-value
TextBox1.Text = result.ToString ();
}


Usually for the above code to work, the delegate needs to reference a method with matching signature. However, in the above code, an anonymous method is defined inline to perform the calculation. Under the hood, again a private method is declared which is invoked by the compiler.

The above is a trivial example; however anonymous methods can be useful in many scenarios such as collections and custom classes. Let us see an example of using an anonymous method within a collection. We first define a custom (my favorite :-) Fruit class:



public class Fruit
{
public Fruit (string n, string d)
{
FruitName = n;
FruitDescription = d;
}

// automatic-property
public string FruitName
{ get; set; }

// automatic-property
public string FruitDescription
{ get; set; }
}


Next we define a generic collection of type Fruit. This collection will let us find a particular fruit using an anonymous method:

Listing 9


protected void Button1_Click (object sender, EventArgs e)
{
string fruitName = "kiwi";

List<Fruit> fruits = new List<Fruit> ();

Fruit f1 = new Fruit ("mango", "A tropical fruit");
Fruit f2 = new Fruit ("apple", "Have an apple a day");
Fruit f3 = new Fruit ("kiwi", "Good for health");

fruits.Add (f1);
fruits.Add (f3);
fruits.Add (f2);

// using anonymous method
Fruit searchFruit = fruits.Find (delegate (Fruit f)
{
return f.FruitName == fruitName; // out of scope
});

if (searchFruit != null)
{
txtname.Text = searchFruit.FruitName;
txtdescription.Text = searchFruit.FruitDescription;
}
else
{
Label1.Text = "Fruit not found...";
}
}


The above example has a few points to note. First, the Find method expects a parameter of type Predicate. This parameter represents a delegate (a boolean expression) used by generic lists to filter and search an element. In the above code, the delegate gap is filled by an anonymous method.

Second, if you watch closely, the anonymous method accesses a local variable (fruitName) which is in the scope of the outer method. How this works is pretty interesting. Earlier in Figure 2 we saw that the compiler generated a method for an anonymous method. But for a variable outside the scope of the anonymous method, the compiler generates a class. The generated method and variable are members of this class. When the anonymous method is invoked, the compiler creates and invokes an instance of this class. All the local variables maintain their state across calls made by the same instance. Figure 3 illustrates this concept:


Fig 3



It is also important to note that an anonymous method cannot access ref or out variables of the outer method.

Returning back to Lambda Expressions, they offer a convenient way to write anonymous methods. Using lambda expressions, we can omit much of the syntactical requirement of an anonymous method. For example, using lambda expression, listing 7 can be re-written as:



protected void Page_Load (object sender, EventArgs e)
{
Button1.Click += (object s, EventArgs ea) => { // implementation }
}


To understand how the above works, you should know that a lambda expression offers the following liberty to developers:

1. The delegate keyword is not required.
2. For a single statement, braces can be avoided. We use the lambda operator => (pronounced as goes to) in place of braces. The left side of this operator represents the input parameters while the right side is the expression block.
3. The return keyword is not required.
4. Since C# 3.0 supports type inference, it is perfectly alright to drop the type definition for variables and let the compiler infer it (a very strong feature of lambda expressions).

Using the above features, let us rewrite listing 8 as following:



public delegate int MathDelegate (int n1, int n2, int n3); // declare delegate

protected void Button1_Click(object sender, EventArgs e)
{
int result;

MathDelegate mathdel = (num1, num2, num3) =>
(((num1 * num2) / num3) + 50); // lambda-expression

result = (int) mathdel (10, 10, 2); // invoke delegate and cast reture-value
TextBox1.Text = result.ToString ();

}


Clearly, the above is a neat and concise expression written using lambda expression. The delegate and return keyword are not omitted. Similarly braces have been avoided and the compiler infers the data type of the input parameters. The same applies to the code in listing 9.

To use a lambda expression, we need a delegate. .NET 3.5 facilitates us with two built-in generic delegate types, Func and Action, so that we don’t have to define our own delegates. The former returns a value while the later does not. In a Func delegate, the last parameter represents the return type. It has the following overloads:

public delegate TResult Func<TResult> ()
public delegate TResult Func<T, TResult> (T t)
public delegate TResult Func<T1, T2, TResult> (T1 t1, T2 t2)
public delegate TResult Func<T1, T2, T3, TResult> (T1 t1, T2 t2, T3 t3)
public delegate TResult Func<T1, T2, T3, T4, TResult> (T1 t1,T2 t2, T3 t3, T4 t4)

Similarly, an Action delegate accepts input parameter(s) but has a return type of void. It has the following overloads:

public delegate void Action();
public delegate void Action<T> (T t1);
public delegate void Action<T1, T2> (T1 t1, T2 t2);
public delegate void Action<T1, T2, T3> (T1 t1, T2 t2, T3 t3);
public delegate void Action<T1, T2, T3, T4> (T1 t1, T2 t2, T3 t3, T4 t4);

To help you understand the above, let me give you a simple example of using the Func delegate with three parameters. The Action delegate is no different (except with no return type).

Listing 10


Func<string, string, string /*return type */> GreetPerson = (message, person) =>
message + " " + person;

protected void Button1_Click (object sender, EventArgs e)
{
TextBox1.Text = GreetPerson (“Hello”, “Scott”);
}


I am sure you can evaluate the above code with ease. Func defines a delegate which accepts two input parameter (message, person) of type string and the last parameter, also a string, as the return type. Clearly, it has simplified the process of defining a delegate. Otherwise we had to define a delegate with matching signature.


Summary

In this post, we looked at different C# language features which make up LINQ. The yield statement lets us implement enumerators without implementing any enumerator interface. Also, the yield maintains state between calls. This is possible since the compiler maintains a state engine.

Extension methods enable us to add functionality to existing types. The types can be primitive or custom. Extension methods are static methods but are invoked like instance methods. These methods are defined in a separate static class. The first input parameter is preceded by ‘this’ keyword which makes it an extension method. This parameter also defines the type on which the extension method will operate.

The var keyword is used to declare implicitly typed local variables. These are strongly typed variables and their types cannot be changed later on. The compiler is responsible for determining the type at runtime. We can either explicitly assign a value or return a value from a method to a ‘var’ variable. These variables must be instantiated with a value so that the compiler can infer the type at runtime.

Anonymous Types facilitate us to create types without actually declaring it. The type is inferred by the compiler at runtime. Anonymous types in turn use a feature known as ‘object initialization’ to work. Object initialization lets us initialize properties without invoking the constructor, when creating objects.

Anonymous methods allow us to use a piece of code inline without defining a separate method. The code is declared and used at the same place. Anonymous methods can be used where a delegate is anticipated. An anonymous method is defined using the delegate keyword followed by an optional list of parameters (within parenthesis) and the body of the method. If an anonymous method doesn’t have any parameters, parenthesis can be omitted. The signature of anonymous method must match with the signature of the delegate being replaced.

Lambda expressions offer a concise way to write anonymous methods. Using lambda expression, we can avoid the extra syntactical requirement of an anonymous method. We can omit the delegate and return keyword. Also, the compiler can infer the data type of the input parameters. To use a lambda expression, a delegate is required. .NET framework has two built in function, ‘Action’ and ‘Func’ respectively, which help us use a lambda expression without actually defining it.

With this we come to the end of this post. To concentrate more on LINQ, I had to sum up all the above concepts in two posts otherwise each feature is worth a separate post. In the next post we will start looking at LINQ Syntax and how it can be leveraged into our code. So stay tuned for more…

Tuesday, February 10, 2009

NHibernate Screencast Series



I have found an excellent series of NHibernate screencasts and thought of sharing it with you. The screencasts are located at SummerOfNHibernate. Hope you find them useful.

Thursday, February 5, 2009

How Microsoft AJAX Client Library is Organized


Recently I ran into confusion about how Microsoft Ajax Client Library is organized. After having my head clear on it, I decided to share my understanding with you. In this post, we will see how the client library is organized across different JavaScript files and Namespaces.

First, the Microsoft Ajax Client Library is organized in JavaScript files. The following three JavaScript files make up the client library:

1. MicrosoftAjax.js
2. MicrosoftAjaxTimer.js
3. MicrosoftAjaxWebForms.js

These JavaScript files are embedded as resources in the Sytem.Web.Extensions assembly. This assembly is installed in the Global Assembly Cache (GAC) by the Microsoft Ajax Extension installer. As you know that every Microsoft ASP.NET Ajax application has one ScriptManager control. It’s this ScriptManager which is actually responsible for loading the JavaScript files in the asp.net page. So we must include the ScriptManager control to enable Ajax functionality in our applications.

Second, the JavaScript files (library) consist of many client classes which are then organized into client namespaces. These namespaces include:

1. Sys
2. Sys.Net
3. Sys.UI
4. Sys.Services
5. Sys.Serialization
6. Sys.WebForms

My confusion was about the difference between the JavaScript files and the Namespaces (client classes). Are these separate or do they complement each other. So here is what I understood after talking to the experts.

The JavaScript files (MicrosoftAjax, MicrosoftAjaxTimer, MicrosoftAjaxWebForms) represent the physical organization (features split in different files) whereas the Namespaces (Sys, Sys.Net, Sys.UI etc) correspond to the logical grouping (client classes into namespaces) of the client library. The namespaces span across the JavaScript files (and so do the client classes). When the JavaScript files are loaded in the web page, the entire client library (namespaces and client classes) gets loaded.

One point worth noting is that the JavaScript files come in two versions – the debug and release version. The ScriptManager always loads the release version (a stripped down version with no comments and debugging tricks) in the asp.net page unless specified. If we want to load a debug version of the script files, we can do so by using the following syntax:



<asp:ScriptManager ID=”ScriptManager1” runat=”server”>
<Scripts>
<asp:ScriptReference Path=”~/ScriptLibrary/ScriptFile” ScriptMode=”Debug”/>
</Scripts>
</ScriptManager>


Summary


In this post, we saw how Microsoft Ajax Client Library is organized. The JavaScript files represent the physical organization of the library. The namespaces represent the logical arrangement of client classes. The JavaScript files have many client classes and these classes are arranged under the different namespaces. The namespace (hence client classes) span across the JavaScript files.

I hope this post was useful in removing any doubts regarding the arrangement of the Ajax Client Library. Stay tuned for more…

Wednesday, December 17, 2008

Understanding ASP.NET MVC Framework

This post will provide you an architectural overview of the ASP.NET MVC Framework. We will explore the underlying concepts which make up the ASP.NET MVC Framework. This post is not meant to getting you started with creating a MVC Application. It rather gives you the background knowledge needed to work with a MVC application. Creating a MVC application will be the topic of a future post.


Introduction


The ASP.NET MVC Framework helps developers implement the Model-View-Controller pattern. The framework provides a clean separation of concerns in a web application. This gives the developers better control over the applications which are easy to maintain.

The MVC Framework is a shift of thinking from the traditional ASP.NET web applications. In an ASP.NET application, a URL points to a resource or content such as a web page or image which is served by the server. The content must exist before being served else an exception may be thrown. On the other hand, in a MVC application, the URL request is handled by a piece of code. This means that every request is handled by a method of a particular class. So there is basic difference between the two programming models. The prior serves existing contents for a request whereas the later runs a piece of code for a request.

Before we proceed, let’s look at Fig 1. This figure shows the solution when we create an ASP.NET MVC Application using Visual Studio. It has different sub folders including Models, Views and Controllers. A discussion of these sub folders will follow shortly. If you expand the References folder, you will find a reference added to System.Web.Mvc nampspace which is required to work with a MVC application.




Fig 1


Controllers

In a MVC application, a Controller is responsible for handling a request. Each URL request is served by a particular controller. It acts as a bridge between the requests and the application.

In Fig 1, you can see a Controllers folder. All controllers are added under this folder. The controller name must be suffixed with the word ‘Controller’. For example, to create a Product controller, we name it as ProductController. If a user enters the URL /Product, it gets mapped to /ProductController. Even if a controller with the name Product exists, it will not respond to a user request. This is the default behavior for an ASP.NET MVC Application.

A controller is just a C# or VB class. This class is driven from System.Web.Mvc.Controller base class. Listing 1 is an example of a ProductController class:


Listing 1


using System;
using System.Web.Mvc;

namespace MVCDemo.Controllers
{
public class ProductController : Controller
{
public ActionResult Index ()
{
// Add action logic here
throw new NotImplementedException();
}
}
}

A controller consists of Controller Actions. Controller Actions or simply actions are the public methods in a controller. Each request is processed by a controller action in a controller. For example, if a user enters the URL /Product/Index, the Index method in the ProductController class is invoked. Any method which is added as a public method to a controller becomes a controller action. Controller actions have certain properties which include:

• They cannot be overloaded
• All actions must be public
• They cannot be static
• An action returns an ActionResult

The outcome (return type) of a controller action is an Action Result. There are different types of action result, all of which are driven from the base class ActionResult. These action results include:

ViewResult: Returns HTML markup or content to the browser
EmptyResult – Represents no result
RedirectResult – Redirect to a new URL
RedirectToRouteResult – Redirects to a new controller action
JsonResult – Returns JavaScript Object Notation result for AJAX applications
ContentResult – Returns a text result

We are not required to return a particular type of action result directly i.e. we don’t return a ViewResult or JsonResult. Rather we use one of the methods provided by the base class Controller. The different base class methods are:

1. View – Returns a ViewResult action result.
2. Redirect – Returns a RedirectResult action result.
3. RedirectToAction – Returns a RedirectToRouteResult action result.
4. RedirectToRoute – Returns a RedirectToRouteResult action result.
5. Json – Returns a JsonResult action result.
6. Content – Returns a ContentResult action result.

Listing 2 shows controller actions return different types of action results:

Listing 2


using System;
using System.Web.Mvc;

namespace MVCDemo.Controllers
{
public class ProductController : Controller
{
// 1 – return ViewResult()
public ActionResult Index ()
{
return View ();
}

// 2 – redirect to another controller action – RedirectToRouteResult()
public ActionResult RedirectMe ()
{
return RedirectToAction ("Index");
}

// 3 – return content – ContentResult()
public ContentResult ReturnContent ()
{
return Content ("Just a simple string...");
}

public DateTime ReturnDate()
{
return DateTime.Now;
}
}
}

The first controller action (Index) returns a view by calling the base class method. The view returned should correspond to name of the controller action. So a view named Index.aspx is returned to the browser. The location of Index.aspx will come up in the next section.

The base class method RedirectToAction in the second controller action (RedirectMe) redirects the request to the first controller action. The request is redirected to Index.aspx.

Another special type of action result is the ContentResult which returns plain text. Using the base class method Content in the third controller action (ReturnContent), a string is returned to the browser. The source of the page will show you a simple text message with no HTML embedded in it.

If a controller action does not return an action result, it is also treated as ContentResult. The MVC framework calls the ToString () method on the result and wraps it in a ContentResult action result. The fourth action controller will again return current DateTime as a string.



Views

As mentioned, in a MVC application, a request does not map to a page or resource. Rather all requests are handled by actions in a controller. An action can return different types of action results including a View. A view is closest to an asp.net web page since it renders HTML markup and content. Listing 2 has one of the following actions:

public ActionResult Index ()
{
return View ();
}

This action returns a view. The action is invoked by typing in /Product/Index which returns a page Index.aspx from the following location:

\Views\Product\Index.aspx

The location of a view is inferred from the name of the Controller and name of the Controller Action. If you look at Fig 1, you will find a Views folder. All views are under this folder. For the above URL, the Views folder must have a sub folder named after the Controller (Product). The sub folder in turn must have a view page named after the controller action (Index in this case). So a view page Index.aspx is returned to the browser. Figure 2 makes it clear:




Fig 2

We can also explicitly mention the name of the view when calling the return View () statement. For example, the statement ‘return View (“Index”)’ is equal to the statement ‘return View ()’.

A View is an HTML document where scripts are written. Writing a view is a kind of a journey back to the Active Server Pages age. An ASP page does not have the notion of a code behind file. All validation, data handling and business logic code are written on the same page. We make extensive use of Response.Write in traditional ASP application. A script written in a View is somewhat similar to an ASP code.

In a view, script delimiters <% and %> mark the beginning and end of a script. We can then use the Response.Write method to emit some data. For example, the following line will write a string:

<% Response.Write ("This is a view script");%>

In order to save coding time, we can also use the shortcut delimiter <%= %> in place of Response.Write. Listing 3 makes use of both delimiters:

Listing 3


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs"
Inherits="MVCDemo.Views.Home.Index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Index View" runat="server">
<title>Index</title>
</head>

<body>
<div>
<% Response.Write ("MVC framework demo"); %>
<br />
Date-Time: <%=DateTime.Now%>
</div>
</body>
</html>

To facilitate the developers, HTML Helpers are used to add content to a view. HTML Helpers are methods that return a string (such as markup). This way we don’t have to type the HTML markup. These HTML helpers generate standard controls including TextBoxes, hyperlinks, dropdown lists etc. The helper methods are called using the HTML property of the view. The following snippet generates a simple Product entry form using HTML Helpers:

<form method="post" action="/Product/Add">
<label for="ProductName">Product: </label>
<%=Html.TextBox ("ProductName")%>
<br /><br />
<label for="Quantity">Quantity: </label>
<%=Html.TextBox ("Quantity")%>
<br /><br />
<label for="Available"> </label>
<%=Html.CheckBox ("Available", "Available")%>
<br /><br />
<input type="submit" value="Add Product" />
</form>

Notice the use of the delimiters with HTML helper method. Since HTML Helper methods return a string, we need to use Response.Write or the shortcut delimiter <% %> to render the string (control markup) in the browser. For this reason, the above snippet has the Html.TextBox (…) helper method enclosed in a delimiter.

Another interesting property of a view is the ViewData property. ViewData is used by controllers to pass data to a view. The ViewData works like a dictionary holding collections of key-value pairs. Let us look at an example:


public ActionResult ProductDetails ()
{
ViewData ["Info"] = "Available in stock";
return View ();
}

In the above snippet, the action ProductDetails uses ViewData property to pass down the key ‘Info’ with a value ‘Available in stock’ to the view. This key can be used later in the view to retrieve the data with the following code:

<%= ViewData ["Info "] %>

A good use of the ViewData property would be to send database records or XML data to a view. The controller action can access the database and pass the data to the view. This also results in a clean separation of concerns where data access is separate from the view. Similarly an action can read a file and send its contents to the view.



Models

The controllers hold logic to handle incoming request and views represent the UI of a MVC application. Where does the business logic and data access code reside? This is where Models come into play. Models are responsible for hosting the business logic. They also hold data access components. For example, we can have DAL (Data Access Layer) classes, Entities or LINQ to SQL classes as models. In fig 1, you can see a Models folder. All data access classes must be placed under this folder.

As a best practice, fat models and thin controllers are recommended. The controller should only be concerned with handling requests whereas the models should deal with business logic and data access code. Even if a controller is passing data to a view fetched from the model, it should be limited to making calls to the model. This also results in a clean separation of concerns in a MVC application.



URL Routing

So how does a piece of code get executed when a request is made in a MVC application? This is where the URL Routing comes into play. URL Routing is a key feature of a MVC application. It is a mechanism through which a URL is mapped to a controller action. URL Routing is handled in the Web.Config and Global.asax file.

The Global.asax handles the application lifecycle events. One of these events is the Application_Start event which is raised when an application starts. Listing 4 shows the Global.asax:

Listing 4 – Global.asax


using System.Web.Mvc;
using System.Web.Routing;

namespace HelloMVC
{
public class GlobalApplication : System.Web.HttpApplication
{
public static void RegisterRoutes (RouteCollection routes)
{
routes.IgnoreRoute ("{resource}.axd/{*pathInfo}");

routes.MapRoute (
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" }
// Parameter defaults
);
}

protected void Application_Start ()
{
RegisterRoutes (RouteTable.Routes);
}
}
}

The Application_Start event calls the RegisterRoutes method. The RegisterRoutes method creates a route table.

The default route table contains of a single route (Default). If you look at above listing, you can see that the Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id. This means that a request such as /Prodct/Add/20 will look for a controller Product with action Add and a parameter to add equal to 20.

The Default route includes defaults for all three parameters. If you don’t supply a controller, then the controller parameter defaults to the value Home. If you don’t supply an action, the action parameter defaults to the value Index. Finally, if you don’t supply an id, the id parameter defaults to an empty string.

Let us see a few examples of URL mapping. Suppose we enter a URL

/Home/Index/2

The parameters for this URL are:

Controller = Home
Action = Index
Id = 2

The code generated for these parameters is

HomeController.Index (3)

Similarly if the URL is

/Product/Delete/19

It is converted to:

Controller = Product
Action = Delete
Id = 19

Now the code generated is

ProductController.Delete (19)

The default route table is fine for a general MVC application. However, we can also create custom routes depending on our routing needs. For example, we are creating an Online Reporting System. This system helps user create and maintain reports. The reports are maintained on a date-by-date basis. The user is allowed to enter a URL like:

/Files/06-19-2008

To handle such request, let us edit Global.asax in listing 5 with our new route defined:

Listing 5


using System.Web.Mvc;
using System.Web.Routing;

namespace MVCDemo
{
public class GlobalApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"OnlineReporting",
"Files/{DateModified}",
new {controller = "Files", action = "Modified"}
);

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" }
// Parameter defaults
);

}

protected void Application_Start()
{
RegisterRoutes (RouteTable.Routes);
}
}
}

The order in which routes are added matters. The OnlineReporting route is added before the default blog. If it is placed after the default route, the default route will always get called before the custom route. Hence our request won’t be handled resulting in an exception.

For the OnlineReport route to work, we must have a Files controller and a Modified action controller. When the Modified method is invoked, DateModified is passed in as parameter. So the code generated is

Files.Modified (DateModified)
Since this method accepts datetime as parameter, the MVC framework converts the parameter to a datetime value.



Summary

In this blog entry, we looked at the architecture of an ASP.NET MVC application. Unlike asp.net applications, a request is handled by a Controller. A Controller is class which can have public methods known as Controller Actions. The request invokes the controller action of a controller. The result of a controller action is of type Action Result. There are different types of action results. We can use methods of the base class Controller to return different types of results.

A view is similar to an asp.net page which renders html markup to a browser. In a view, delimiters <% %> mark the start and end of a script. To render markup on a page, HTML Helper methods are available in a view. These helper methods rid us of writing markup code by rendering controls on a page. The ViewData property of a view is used to pass down information from a controller to a view.

Models take care of business logic and data access in a MVC application. The models can consist of DALs, Entities and LINQ to SQL classes. To maintain clean separation of concern, we should favor fat models and thin controllers.

URL Routing is another important feature of a MVC application. It handles and supports user friendly URLs. To handle URLs, a route table is created in the Global.asax file using the Applicatin_Start event handler. This table defines the parameters for a URL which includes the controller, the controller action and any id passed to the controller action. Finally we can also setup custom routes to handle any special routing need.

I hope this post has given you sufficient insight into ASP.NET MVC architecture. In a future post, I will talk about creating and working with a MVC application. So stay tuned…

Thursday, December 11, 2008

LINQ Explained– Part 2

This is the second part of my on going series on LINQ. In the first installment, we had an overview of LINQ. In this post, we will look at some of the underlying concepts which are important to understand to work with LINQ. Though you are not required to master them but an understanding of these concepts gives you the extra level of confidence to work with LINQ. (Note: I will use C# as language of preference in this series).

LINQ and C# Language

LINQ works with C# 3.0 and Visual Basic 9.0. It relies heavily on the features provided by these languages. Although these features may be used separately, they are fundamental to the working of LINQ. Some of these features were delivered with C# 1.x and 2.0 – the predecessor to C# 3.0. The following sections describe these features in more detail.

Generics

Let us look at a simple example of comparing two numbers. The following method accepts two integers as argument, compares them and returns the result:

private int Compare (int x, int y)
{
return x < y ? x : y;
}

This code works fine for comparing two integers. But suppose we wanted to compare two floating values or even two strings. For this purpose, either we change the method signature to accept the respective types or we end up writing entirely new methods for each type. But as developers, we would be inclined towards using a generalized method to perform the same function irrespective of the argument types.

One solution to the above problem is to accept object as arguments. In C#, every type is driven from the base type object so it can be cast to and back from the object type. We can rewrite the above method as following:


private object Compare (object x, object y)
{
// comparison logic goes here
}

The above method now accepts object as argument. This makes the method more generalized but with some shortcomings. First of all, C# is a type-safe language, that is, objects have associated type and only operations defined by the associated types can be performed on an object. A comparison operator such as ‘<’ will not operate on the reference type object. Thus a conversion of object to a value type is required before the comparison is performed. This means to use a type, explicit casting required. For example, to convert to an integer, we write the following code:

int i = (int) x;
int j = (int) y;

Similarly, to check for string, we perform the following casting:

string str1 = (string) x;
string str2 = (string) y;

Second, when a value type is converted to a reference type (boxing), it involves an overhead. A conversion back to the value type from a reference type (unboxing) also involves an overhead. As a result of boxing and unboxing, there is a performance hit for an application. This is further magnified when Collections are used. Collections such as Stack, Queue, ArrayList operate on object type only. When an element is added to a collection, boxing takes places. Similarly, when a value is retrieved from the collection, unboxing is done. This means every time an element is added or retrieved from a collection, a performance overhead is involved.

The above issues can be catered through Generics. A C# 2.0 language feature, Generics introduced the concept of type parameters. Classes and methods can defer the definition of one or more type until runtime. With Generics, there is one implementation for all types. The type definition is performed when the method is invoked or an object is instantiated. Let us redefine our Compare method using Generics:



private T Compare <T> (T x, T y) where T : IComparable <T>
{
return x.CompareTo (y) < 0 ? x : y;
}

A placeholder <T> appended to the method name points to a generic method. The placeholder <T> represents the type parameter to be provided when this method is used. The same placeholder is used for the input and output parameters. Note that since <T> is just a placeholder (and not a type), the CompareTo method cannot operate on it directly. For this reason, <T> implements the IComparable interface. We will look at the ‘where’ constraint shortly. For now we can use the above method for the comparison of different types as following:

int a = 20, b = 19;
int c = Compare <int> (a, b);

string str1 = "Zzzz", str2 = "Aaaa";
string str3 = Compare <string> (str1, str2);

Notice that there is no explicit casting required for the arguments. The method is invoked with the type parameter inplace of the placeholder and that’s it. The CLR is responsible for handling the rest.

The Generic concept also applies to classes and structures. Let us look at a Generic class:




public class UserAuthentication <T>
{
private T myPassword;
private string myUserID;

public T Password
{
get { return myPassword; }
set { myPassword = value; }
}

public string UserID
{
get { return myUserID; }
set { myUserID = value; }
}
}

The above class creates a token for user authentication. Notice the placeholder <T> defined next to the class name. The password field is of the same type parameter. Similarly the Password property has a returns type of <T>. We can instantiated the above class with following code:


UserAuthentication <string> userAuth;

userAuth = new UserAuthentication <string> ();
userAuth.Password = "Secret";
userAuth.UserID = "User1";

UserAuthentication <int> userAuth;

userAuth = new UserAuthentication <int> ();
userAuth.Password = 123456;
userAuth.UserID = "User2";

.NET framework supports different generic collections under the System.Collections.Generic namespace. These generic collections include:

Stack <T> - a generic collection representing a Last-In-First-Out collection
Queue <T> - a generic collection representing a First-In-First-Out collection
List <T> - a generic collection of strongly type object list
Dictionary <K, V> - a generic collection of key-pair values

Let us see a generic List in action which accepts a strongly-typed parameter. We first define the strong-type Product followed by the generic list:



public class Product
{
string productName;

public Product (string pName)
{
productName = pName;
}

public string ProductDetails
{
get
{
return "Product-Name: " + productName;
}
}
}

public class ProductList <T> : IEnumerable <T> where T : Product
{
List <T> productList = new List <T> ();

public void AddProduct (T product)
{
productList.Add (product);
}

public T GetProduct (int index)
{
return productList [index];
}

IEnumerator <T> IEnumerable <T>.GetEnumerator ()
{
return productList.GetEnumerator ();
}

IEnumerator IEnumerable.GetEnumerator()
{
return productList.GetEnumerator ();
}
}

We can now use the ProductList class to add and list products (a discussion of IEnumerable will follow shortly):


ProductList <Product> productList = new ProductList <Product> ();

productList.AddProduct (new Product ("Rice"));
productList.AddProduct (new Product ("Milk"));
productList.AddProduct (new Product ("Sugar"));

… = ((Product) productList.GetProduct (1)).ProductDetails;

Before I sum up the generics discussion, one last thing worth mentioning is constraints. If you have noticed in the ProductList class (and the Compare method), there is a use of ‘where’ constraint. A constraint is a condition applied on the type parameter. We can use constraints to treat only specific types. For this reason the constraint ‘where T : Product’ is added to the class definition. This way we create a generic list which only deals with Product objects. We can have the following different constraints attached to the generic type:

where T : class – type parameter is a reference type
where T : struct – type parameter is a value type
where T : new () – type parameter with a default constructor
where T : interface – type parameter implements an interface


Delegates

A delegate is an object which holds a reference to a method. When the delegate is called, the underlying method is invoked. This way a delegate behaves exactly like the referenced method. The method can either be static or an instance method. A delegate defines the method signature and any method with matching signature can be reference by the delegate. This makes it possible to change the reference to a different method programmatically and update the code in the methods without modifying the delegate. This simple concept of abstraction adds lots of power to the .NET Framework (A detailed discussion of delegates is beyond the scope of this post. A detailed post or two would cover delegates, events, asynchronous callback and threading in the future).

Working with delegates is a pretty simple in C#. Always keep in mind the method signature when defining a delegate. Let us look at the syntax of defining a delegate:

delegate result-type Name (parameters);

The delegate keyword is used as prefix to define the delegate. The result-type reflects the return type from the referenced method. The Name is the identifier of the delegate and the optional comma separated parameters are the input argument to the referenced method. The result-type and parameters define the signature of the delegate. Using the above syntax, we can create a delegate as following:

public delegate void Calculate (int value, int amount);

Any method with matching signature can be referenced by the above delegate. Let us define a method with the matching signature:



public class Accounts
{
public void DebitAccount (int x, int y)
{
int sum;
sum = ((x * 10) / y) * 2;
// use sum…
}
}

We can now instantiate and invoke the delegate by referencing the DebitAccount method as following:

Accounts objAccount = new Accounts ();
Calculate calc = new Calculate (objAccount.DebitAccount); // reference method
calc (2, 3); // call delegate

When we call the delegate, the DebitAccount method is invoked.

Multicasting is one of the features provided by delegates. A multicast delegate can reference more than one method at a time. When the delegate is called, all the referenced methods are invoked. The methods are invoked in the order in which they are referenced by the delegate. Let us modify the Accounts class by adding the following method to it:



public void CreditAccount (int x, int y)
{
int average;
average = ((x / 2) + 10) - y;
// use average…
}

The calc delegate can now reference the above method using the compound assignment operator (+=):

calc += objAccount.CreditAccount;

Now if the call the delegate using calc (2, 3), both methods get invoked. Once you have used a delegate, the reference must be released. References can be removed using the compound subtraction statement or null value assignment as following:
calc -= objAccount.CreditAccount; // remove reference to CreditAccount
calc = null; // remove all references

Delegates are also used for Asynchronous Callbacks. When asp.net receives a request for a page, it assigns a thread from the thread-pool to the requested page. In a synchronous call, the page holds on to the thread for the duration of the request, blocking calls to the thread for new requests. This is acceptable for a short lived request but if the request is time-bound such as calling multiple web services or an I/O bound job, the delay is annoying.

Delegates help perform asynchronous tasks using Method Callback. With this technique, the delegate invokes the time-consuming method in a separate thread and the control returns immediately. The time-bound task executes in the background while we can continue with our processing. When the background job is finished, control is transferred to a callback method which can handle the result and update any control. Let me demonstrate this concept with an example. We first write the time-consuming process as following:



// the time consuming process
public bool LongProcess (int wait)
{
// your lengthy task goes here
System.Threading.Thread.Sleep (wait); // just for demonstration
return true; // return the result
}

Next we declare a delegate and use it to invoke the above method.


// define the delegate
public delegate bool LengthyProcessDelegate (int wait);

User clicks on a button to start the process:


// Use the delegate to start the lengthy process
protected void StartProcessing_Click (object sender, EventArgs e)
{
LengthyProcessDelegate lDelegate = new LengthyProcessDelegate (LongProcess);
lDelegate.BeginInvoke (5000, new AsyncCallback (LongProcessCallback), lDelegate);
for (int i = 0; i <= 50; i++)
{
// do something
}
}

The above code first creates a new instance of the delegate which holds rerference to the time-consuming method. Next it calls the BeginInvoke method using the delegate. You must be wondering what this method is? Remember, when we declare a delegate, the compiler generates code similar to the following:


class LengthyProcessDelegate : System.MulticastDelegate
{

// synchronous execution
public bool Invoke (int wait);

// asynchronous execution methods
public IAsyncResult BeginInvoke (int wait,
AsyncCallback callback,
object asyncState);

public bool EndInvoke (IAsyncResult result);
}

The Invoke method is used for sychronous calls. The other two methods, BeginInvoke and EndInvoke handle the asynchronous activity.

BeginInvoke method returns an instance of interface IAsyncResult. It accepts the same arguments as defined by the delegate plus two additional optional parameters. The first parameter is an instance of AsyncCallback (another delegate) which references the callback method. The second parameter is of type object which can be used to pass any information.

The EndInvoke method has the same return type as defined by the delegate. It accepts an instance of IAsyncResult. As mentioned above, BeginInvoke returns an instance of IAsyncResult. This instance is passed down to the callback method which is used by the EndInvoke method as parameter. It in turn returns the result of the time-consuming method which can be used for further processing. Let us look at the callback method in action:



// Callback method
public void LongProcessCallback (IAsyncResult result)
{
LengthyProcessDelegate lDelegate= (LengthyProcessDelegate) result.AsyncState;
bool returnValue = lDelegate.EndInvoke (result);
// use returnvalue
}

As mentioned above, when BeginInvoke is called, the delegate invokes the callback method in a separate thread and the control returns to the program immediately. If you have noticed above, I have got a dummy loop after the call to BeginInvoke method. This is just to show you that the processing will continue and not wait for the time consuming process to complete.

delegates also provide a rich programming model to handle Events. An event lets an object notify the program when its state changes. Events allow objects to provide noification to be responded. This simple concept is very important for inter-process communication where the change of state of one object signals other objects to respond. A good example of events is a Graphical User Interface. The program transfers the control to an event handler when an event such as Button-Click is triggered by the user action. Another example would be an Accounts object raising an event when a transaction is made.

In C# events and delegates go hand-in-hand. Any object which triggers an event isn’t aware when the event is raised. This is left to a delegate which act as a bridge between the object and the event. Let us see this concept with a simple example. We begin with defining a delegate:

// define the delegate
public delegate void AccountDelegate (); // no input, output parameters

Next we define an Accounts class. This class has a Transaction property which fires an event when its value changes. The event is defined using the AccountDelegate. Since the delegate’s signature does not have any input or output parameter, the event handler for the event will have a similar signature. The event handling method OnTransactionOccur is defined as virtual which can be overridden by derived classes.



public class Accounts
{
private int amount;
// define the event
public event AccountDelegate transactionComplete;

public int Transaction
{
get { return amount; }

set
{
if (value <= 100)
amount--;
else
amount++;

OnTransactionOccur (); // raise the event
}
}

protected virtual void OnTransactionOccur ()
{
if (transactionComplete != null)
transactionComplete ();
}
}

We can now raise the event with the following code:


public void StartProcessing_Click (object sender, EventArgs e)
{
Accounts account = new Accounts ();
// register the event
account.transactionComplete += new AccountDelegate (AccountEventHandler);

account.Transaction = 100; // raise the event
}

First we instantiate an object of Accounts class. Next we register the event using the delegate with the event handler AccountEventHandler. We then set the Transaction property to raise the event. Remember the base class method OnTransactionOccur is actually responsible for raising the event. As soon as the event is raised, the control is transferred to the following event handler.


public static void AccountEventHandler ()
{
// event handling code goes here
}

Enumerators

Enumeration, a powerful .NET concept, allows us to iterator through a collection of objects. In .NET, enumerators are based on the Iterator Pattern. Using this pattern, we can access elements of an aggregate (combination of many elements) object without revealing the inner working. The terms Enumerators and Iterators are used interchangably but .NET uses the term Enumerator.

A class must implement the IEnumerable interface to provide iteration. This interface exposes the following single method:



public interface IEnumerable
{
IEnumerator GetEnumerator ();
}

The GetEnumerator method returns an object of IEnumerator interface. This object does the actual iteration on our collections. According to MSDN, Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection. Enumerator interface exposes the following methods:


public interface IEnumerator
{
bool MoveNext();
object Current{ get; }
void Reset();
}

When we implement our own enumerators using the IEnumerator interface, the enumerator is positioned before the first element initially. To read the first (and subsequent) element, we use the MoveNext method. MoveNext method returns true until the end of the collection is reached. When MoveNext reaches the end of the collection, it returns false. To get the active element, we use the Current method. The Reset method positions the enumerator before the first element.

Let me demonstrate the above concept by a simple example. I begin by defining (beaten to death :-) Product class as following:



public class Product
{
private string productID;
private string productName;

public Product (string id, string name)
{
productID = id;
productName = name;
}

public override string ToString()
{
return String.Format ("Product details are ID: {0}, Name: {1}", productID,
productName);
}
}

Next we define our Custom Collection class which implements IEnumerable interface:


public class ProductCollection : IEnumerable
{
private ArrayList productList;

public ProductCollection ()
{
productList = new ArrayList ();

productList.Add (new Product ("P1", "Tea"));
productList.Add (new Product ("P2", "Beverage"));
productList.Add (new Product ("P3", "Milk"));
}

public IEnumerator GetEnumerator ()
{
return ((IEnumerable) productList).GetEnumerator ();
}
}

I have used an arraylist to define a product collection. Since the arraylist already implements the IEnumerable interface, we can get hold of the its enumerator object by calling its respective GetEnumerator method. The enumerator object can be used with foreach loop to provide enumeration:


public void StartProcessing_Click (object sender, EventArgs e)
{
ProductCollection collection = new ProductCollection ();

foreach (Product p in collection)
// ListBox1.Items. Add (p.ToString ()); - ading to a listbox
}

The foreach statements simplifies the enumeration code for us. Under the hood, when we use the foreach loop, the compiler generates an initial call to GetEnumerator. It then uses MoveNext for each iteration to get the current item. Since the enumerator is positioned before the first element, the compiler doesn’t have to call the Reset method.

We can also create our own Enumerator class by implementing the IEnumerator interface. Let us modify our ProductCollection class with a nested class as following:



public class ProductEnumerator : IEnumerator
{
private ProductCollection productCollection;
private int index;

public ProductEnumerator (ProductCollection collection)
{
productCollection = collection;
index = -1;
}

public void Reset()
{
index = -1;
}

public object Current
{
get
{ return productCollection.productList[index]; }
}

public bool MoveNext()
{
index++;
if (index >= productCollection.productList.Count)
return false;
else
return true;
}
}

Here is the tricky part. We used the GetEnumerator method of the ArrayList to get an enumerator object. We will modify that code to return an instance of our custom enumerator as following:


public IEnumerator GetEnumerator()
{
return (IEnumerator) new ProductEnumerator (this);
}

One last thing worth mentioning are generic enumerators. These enumerators are used for a generic collection. The two interfaces are IEnumerable<T> and IEnumerator<T> found in System.Collections.Generic namespace. Both these interfaces inherit from their counterpart IEnumerable and IEnumerator. This means that generic enumerable objects are available both generically and non-generically. Let us first look at the IEnumerable<T> interface:


public interface IEnumerable<T> : IEnumerable
{
IEnumerator<T> GetEnumerator();
}

IEnumerable<T> inherits from IEnumerable. This means any collection implementing IEnumerable<T> interface must define a generic and non-generic version of GetEnumerator method. So a generic collection class will have the following two implementations of GetEnumerator method:


public IEnumerator<T> GetEnumerator()
{
return new Enumerator<T>(this);
}

IEnumerator IEnumerable.GetEnumerator()
{
return new Enumerator<T>(this);
}

The same concept applies to IEnumerator<T>. This interface is defined as following:


public interface IEnumerator<T> : IDisposable, IEnumerator
{
T Current { get; }
}

IEnumerator<T> interface implements IDisposable and IEnumerator interface. It has only one property. So any class implementing IEnumerator<T> inherits the rest of the members from IEnumerator and Idisposable interfaces.

Summary

In this post, we looked at some of the underlying concepts which help us better understand how LINQ works. Generics have introduced the concept of type parameter where the type definition is delayed till an object is instantiated. A place holder <T> defines a generic type. Generics apply to methods, classes and structures. We can apply constraints to our generic type to accept fixed type parameters. The .NET Framework ships with built in generic types such as Queue <T>, Stack <T> to reduce the development overhead.

Another feature important to understand LINQ is delgates. Delegates are objects which hold reference to methods. A call to the delegate invokes the reference method. Delegate can also hold reference to multiple methods. This property is known as multicating. Delegates are also used for asynchronous callbacks. Using a delegate, a callback method is attached to a long running process. After the process has finished, the delegate transfers control to the callback method which can retrieve the result and process it. Delegates also move hand-in-hand with events which notify us of a change. We can then write our event handlers to responsd to these changes.

We also looked at enumerators. Enumerators are used to iterate through the elements of an aggregate object. All enumerators implement IEnumerable and IEnumerator interfaces to provide iteration. The foreach comes in handy to iterate through a collection. Generic collections can also take advantage of enumerators by implementing IEnumerable <T> and IENumerator <T> interfaces.

When I sat down to write this post, I thought of explaining all the underlying concept in this post. But each topic is worth a separate post. For this reason, I will sum up the rest of the concept in the next post. Please do provide your feedback on this series and stay tuned for more…