Wednesday, July 15, 2009
Partial Types in VS.Net 2005
There is no modification done in the CLR for partial classes. The implementation of partial classes is done by the compiler for VB.Net and C# in Visual Studio .Net itself. For the CLR, whether you implement the code using partial classes or the class being in a single file, is the same. The compiler, when compiling the code in VS.Net 2005, looks for the partial classes and combines them so that the end result will look as if the code were written in a single class. After compilation the IL produced will look the same for both the implementation.
In Visual Studio .Net 2005, if you try to write some code for an event of a control, in the code-behind file now you will not see the codes that were automatically generated by VS.Net. Instead you will see the code-behind file for an .aspx page which will be a partial class for that page. Since, a class file can be slit across different files, the automatic code generated are placed in a different file which will have a partial class for that .aspx page. Thus, you will see very little code in the code-behind file.
Partial classes are declared using the “partial” keyword. Let us look at the sample code that is given below in C#, which implements a class as partial classes in different files.
//FirstFile.cs
using System;
interface IPartialClass
{
void PrintAString(string s);
void PrintAString();
}
partial class MyPartialClass
{
private string s = "Variable from a separate Partial Class.";
}
class MainClass
{
static void Main(string[] args)
{
MyPartialClass PClass = new MyPartialClass();
PClass.PrintAString("String from a Partial Class.");
PClass.PrintAString();
Console.ReadLine();
}
}
//SecondFile.cs
using System;
public partial class MyPartialClass : IPartialClass
{
public void PrintAString(string str)
{
Console.WriteLine(str);
}
}
//ThirdFile.cs
using System;
partial class MyPartialClass
{
public void PrintAString()
{
Console.WriteLine(s);
}
}
The code given above has three different files, FirstFile.cs, SecondFile.cs, and ThirdFile.cs. The first file has an interface IPartialClass declared in it. This interface is implemented in two other files, SecondFile.cs and ThirdFile.cs. You may note that the method PrintAString(string str) is implemented in the SecondFile.cs and the method PrintAString() is implemented in the ThirdFile.cs. You may note that the string variable ‘s’ is declared in the FirstFile.cs. The IL produced after compiling this code in VS.Net 2005 will look the same as if compiled in earlier versions of VS.Net 2005 and the CLR finds no difference in the IL produced.
If you are not implementing all of the methods in the interface in the partial classes, you will get a compilation error stating that you have not implemented all of the methods. This is the same type of error that you get if you have a single class in a single file.
The PrintAString method in the above example is an overloaded method. A unique overloaded method is implemented in the SecondFile.cs and the ThirdFile.cs respectively. If the signature for the overloaded method in different files does not differ, you will get an error stating that the signature are the same, which is again the same type of error that you get when you have a single class instead of partial classes.
The intellisense in Visual Studio .Net 2005 recognizes the partial class and the implementation of the interfaces across different files. You may note that the private string variable declared in the FirstFile, has the scope in the ThirdFile.cs in which that string variable is actually used.
This type of splitting the class across different files, help in increasing the productivity, as a single big class running into thousands of lines of code can be split into different files. Each of these file can have a different functionality to be developed, which can be given to different developers.
Sunday, July 12, 2009
Understanding Caching in ASP.Net
Caching is a feature in ASP.Net that is very useful in creating dynamic web pages. You know that dynamic web pages contain content that is constant changing and they may even vary according to the users. For example a simple query to return a list of products from an online store will return the products based on the query the user inputs.
The user would have requested the products from a particular manufacturer or products within a particular range of price. The output of such queries will vary on the user’s input to the query engine.
Moreover apart from the results of the query the looks of the page, the menus, and other links will not change. The header and the footer will not change. Hence if you cache the part of the part that provides you the data is enough. Such types of caching are also supported in ASP.Net. We will look at the different types of caching supported by ASP.Net. if the page content of the webpage is similar for each request by any user you can use caching to produce dynamic pages that are fast loading.
In simpler terms the cached page is stored in the memory of the server and this page is served from the memory without generating the page. Thus faster response is provided for the requests made to a cached page. Caching can be done in to three types. One is Page caching, which enables you to cache the entire page, the other is caching the page elements, and the Data caching. When you cache the page elements, some of the elements of the page are cached. In data caching the data that is available in the page is cached.
In Page caching, the entire page is cached. To achieve this you have to use the @OutputCache directive at the top of the page that is to be cached. The syntax of this directive is as given below:
<%@ OutputCache Duration=6 VaryByParam="id" VaryByCustom="browser" %>
The attributes of this directive are Duration, VaryByParam, and VaryByCustom. The Duration directive is used to specific how long the output of the page should be cached. After the specified duration in this attribute the cache of this page is removed and the page will be generated for the next request. The value of the Duration attribute is in seconds. Let us say that you have specified 6 as the value for the Duration attribute.
Then the page output will be cached for 6 seconds before it is removed from the cache. After 6 seconds the page will be generated for the next request. The VaryByParam attribute specifies the querystring parameters to vary the cache. This is a compulsory attribute. If the value of this attribute is ‘None’ then the page output that is delivered is the same whatever be the parameters for the querystring. For example if you specify the value of the VaryByParam value as ‘category’, then the request to this page will be given an output based on the query string ‘category’. The directive for such a page will look like,
<%@ OutputCache Duration=6 VaryByParam="category" %>
and the request to the page would look something like,
http://www.yoursite.com/webpage.aspx?category=3
http://www.yoursite.com/webpage.aspx?category=4
The VaryByCustom tag is used to display content based on the browser that is used by the user. This is used to give page output for different browsers in a different manner.
You might know that the header and the footer for a web page do not change in most of the web pages. To just cache the header and the footer you can use techniques called Fragment caching. This allows a particular part of the web page to be served from the cache. For fragment caching you have to put the content that is to be cached in a user control and give the caching directive at the top of the user control and not in the page that contains the user control. This caches the user control for a particular duration while the page serves the dynamic content.
Data Caching is used to cache objects into the memory and later them and use it in the web application. This enables you to use the cached object across all the pages of the application. The lifetime of such cached objects is that of the application itself. If the application is restarted then all the cached objects are destroyed. A simple example of how to cache is given below:
Cache["company"]="ABC Inc";
The above code stores the company name in the cache. To retrieve this value you have to first check whether that object is available in the cache and then use it.
if (Cache["company"] != null)
Label1.Text= Cache["company"].ToString();
You can also use the Add methods or the Insert methods of the Cache class to insert objects into the Cache class. The following example show you how to use the insert method of the cache class.
Cache.Insert("company", sCompany, NULL,
DateTime.Now.AddMinutes(3), TimeSpan.Zero);
The first two parameters of the insert method are key and the object that have to be inserted into the cache. The third parameter is called the CacheDependency that can be used to set dependency to a file. Null indicates that there is no dependency. The next parameter specifies the time at which the object has to be removed from the cache. The last parameter is called the sliding expiration parameter which shows the time interval after which the object is to be removed from the memory.
Thus by using the Caching features of ASP.Net you can provide powerful dynamic pages in a web application which provides faster response to the requested pages.