Archive for April, 2010

Diffrence between Abstract class and Interface

Feature Interface Abstract class
Multiple inheritance A class may inherit several interfaces. A class may inherit only one abstract class.
Default implementation An interface cannot provide any code, just the signature. An abstract class can provide complete, default code and/or just the details that have to be overridden.
Access Modfiers An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public An abstract class can contain access modifiers for the subs, functions, properties
Core VS Peripheral Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface. An abstract class defines the core identity of a class and there it is used for objects of the same type.
Homogeneity If various implementations only share method signatures then it is better to use Interfaces. If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.
Speed Requires more time to find the actual method in the corresponding classes. Fast
Adding functionality (Versioning) If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Fields and Constants No fields can be defined in interfaces An abstract class can have fields and constrants defined

Configuring and Deploying ASP.NET Applications

Asp.net applications can be easily configured by using web.config files. You can store number of things in the configuration file. Let’s see some important stuff that can be stored in this file.

Database Connections

//

The most important thing to store in the web.config file is the database connection string. The reason of storing connection string in the web.config file makes sense since if later we ever want to change the location of our database we just have to change the connection string in the web.config file and thats it. This will certainly save us a lot of alteration in different files where we used the old connection.

Lets see a small example of the connection string which is stored in the web.config file.

< configuration >

< appSettings >

< add key=”ConnectionString ” value=”server=localhost;uid=sa;pwd=;database=DBPerson” / >

< / appSettings >

< / configuration >

As you can see its really simple to store the connection string in the web.config file. The connection string is referenced by a key which in this case is “ConnectionString”. The value attribute of the configuration file denotes the information about the database. Here we can see that if has database name, userid and password. You can define more options if you want.

There is a very good website that deals with all sorts of connection strings. Its called “www.connectionstrings.com , in the website you will find the connection strings of all sorts of databases.

lets see how we access the connection string from our Asp.net web application.

using System.Configuration;

string connectionString = (string )ConfigurationSettings.AppSettings[“ConnectionString”];

As you see its very simple to get the connection String out from the web.config and than use it in your application.

Session States

Session in Asp.net web application is very important. As we know that HTTP is a stateless protocol and we needs session to keep the state alive. Asp.net stores the sessions in different ways. By default the session is stored in the asp.net process. You can always configure the application so that the session will be stored in one of the following ways.

//

1) Session State Service

There are two main advantages of using the State Service. First the state service is not running in the same process as the asp.net application. So even if the asp.net application crashes the sessions will not be destroyed. Any advantage is sharing the state information across a Web garden (Multiple processors for the same computer).

Lets see a small example of the Session State Service.

/>

The attributes are self explanatory but I will go over them.

mode: This can be StateServer or SqlServer. Since we are using StateServer we set the mode to StateServer.

stateConnectionString: connectionString that is used to locate the State Service.

sqlConnectionString: The connection String of the sql server database.

cookieless: Cookieless equal to false means that we will be using cookies to store the session on the client side.

full details

http://www.exforsys.com/tutorials/asp.net/securing-asp.net-applications-with-csharp.html

Caching in asp.net

Caching is one of the coolest features in Asp.net. Caching enables you to store the expensive data into Cache object and later retrieve it without doing expensive operations. A very common example where you want to use caching is datagrid paging. I am sure you all are familiar with datagrid paging which enables you to view the records in multiple pages. Each time you visit a different page all the records are fetched from the database. This becomes very expensive operation. Caching can save a lot of expensive operations since you can store all the records in the cache object and use the cache object as the data source. In this article we will see some important features that caching provides.

Output Caching:

Output caching is used for pages and is also known as Page-level caching. All you need to do to enable output caching is to add a directive in your html view of the aspx page. The output directive can be written like this:

@ OutputCache Duration=”60″ VaryByParam=”none”

The page will be cached for 60 seconds the VaryByParam attribute is set to “none” which means that there is no sort of caching implemented. Hence if the first user requested page which contains item1 than the second user will also see item1 even if he is requesting item2.

That’s why we always specify what the caching depends on.

@ OutputCache Duration=”60″ VaryByParam=”Category”

In the above OutputCache directive you can notice that I changed the VaryByParam attribute to “Category” which means that now the result of caching depends upon Category. Here Category represents the name of the column in the database.

Programmatic Page Caching

You can also use caching programmatically, meaning that you can change the value of cache depending upon the tasks performed by the user. The Response.Cache class let’s you access the functionality to work with the cache object.

You can change the expiration time on the Cache using the SetExpires method of the Response.Cache class.

Response.Cache.SetExpires(System.DateTime.Now.AddMinutes(10));

In the same way you can also use Response.Cache.VaryByParams to set the Params programmatically.

Accessing Caching in the Class Libraries

Sometimes you will need to access the caching object in the class library. You cannot use Response class anymore since its only limited to the asp.net code behind page. For accessing cache in class library you will have to use HttpContext.Current.Cache. Also don’t forget to include System.web namespace.

Data Caching

Caching is one of the coolest features in Asp.net. Caching enables you to store the expensive data into Cache object and later retrieve it without doing expensive operations. Data Caching can tremendously increase performance since each time the data is requested you can turn to the Cache object rather than going to the database and fetching the result.

//

You all must be familiar with datagrid paging where you can display certain number of records in the datagrid control and use the numeric or next-previous buttons to view the other records. All the records are fetched for each and every page in the datagrid. Meaning that if you have 40000 records in the database and you are using paging. Each time you click to go to the next page you retrieve 40000 records. This is way too much performance kill. That’s why for this task we can use Data Caching, let’s see how we can use data caching to make our application more efficient.

private void Button3_Click(object sender, System.EventArgs e)

{

if(Cache[“MyArticles”] == null)

{

// Go to the database and fetch the result in the DataSet

// Assign the dataset to the Cache object

// Cache[“MyArticles”] = ds

}

else

{

// This means that Cache is not empty and there is data in the cache

// Extract the value of Cache object to the DataSet

// DataSet ds = (DataSet) Cache[“MyArticles”]

}

}

The above example is pretty much simple and as you have also noticed that the syntax for using Data Caching is very similar to the ViewState object. By using this technique you will only get the data from the database if the Cache is empty. And if you see the page source you will not find any hidden fields since Cache is stored in memory rather than in page source.

Caching Page Fragments

Fragment Caching refers to caching the sections of the page. These sections are most commonly UserControls. Page fragment caching allows you to cache the small portion of the page instead of caching the whole page.

Let’s see some examples of fragment caching and how it can be used.

@ OutputCache Duration=”120″ VaryByParam=”CategoryID;SelectedID”

In the Page directive above we have cached CategoryID and SelectedID for 120 seconds. Both of these are the query string parameters.

This means that if the first user request CategoryID = 2 and the second user request the same CategoryID than the second user will recieve the contents from the cache object instead of going to the database and pulling records.

@ OutputCache Duration=”120″ VaryByParam=”none” VaryByControl=”Category”

The VaryByControl attribute can only be used in fragment caching. You can use this to cache the value of the controls. These controls can be any server controls like dropdownlist or datagrid.
When using fragment caching you only need to put the cache directive in the user control and not on the page.

You can use different type of tools to monitor your performance before and after caching is used. Some of the good tools are NProf and ACT