Pages

Tuesday, June 21, 2011

Caching

What is Caching?
      - It is a technique used to store the data and web pages temporarily in the main memory, which could be used later.
     - As it minimizes the usage of server resources, it improves the performance of the application.

Why Caching?
     - Fetching data form the database is one of the slowest web site operations.
     - Each time the request is made, it undergoes "Client -> Server -> Database -> Server -> Client" cycle. This is very time consuming. And depending upon the data density, the time taken would increase.
     - This process affects the performance much, when many people access simultaneously.
     - Here when Caching concept is applied, we could improve the performance.
     - By using caching the database data could be cached in memory and could avoid the database access with every request.

Types of Caching:
     - Page Level Output Caching.
     - Data Caching
     - Fragment Caching.

Page Level Output Caching:
    - Mainly used for static pages.
    - Advantages - Simple to implement, and are sufficient in many cases.
    - It catches the output of the page and gets stored. This avoid the generation of page for each request.
    - It just stores a copy of HTML in memory, that was sent to response on a request.
    - To implement add OutputCache directive on to the page, which needs to be cached.
         Code Snippet:
        &lt%@ Page Language ................... % &gt
        &lt@ OutputCache Duration="10" VarByParam="none" %&gt
    - Here this page would be cached for 10 sec
    - when the cache is refreshed repeatedly this time remains constant until the cache is expired.

Page Fragment Caching:
    - It stores only the part of the page to be cached.
    - Ex, when the user logs in, his user name is displayed in header till he logs out. thus this header portion alone could be cached using this technique. For this the header need to be defined in user control.
    - To implement, add OutputCahce directive in the user control page that has to be cached.
         &lt%@ OutputCache Duration="30" VarByControl="Header"  %&gt
    - Here this header user control would be cached for 30 sec.

Data Caching / Application Caching:
    - It is much more powerful than the other two.
    - Here a part of the data is been cached and stored as object. Its scope is that of the application.
    - It is done using "cache" class. Here an item is cached under a name and retrieve it with the same name when required.
    - Example:
           For storing ---  Cache["ID"] = EmployeeID
           For Retrieving - EmployeeID = Cache["ID"]

Where does the Cache data gets stored??
    - The location of the cached data could be specified in the OutputCache directive.
    Any:
         - This stores the output cache in the client's browser, on proxy server (or any other server) that participates in the request.
         - By default Any is selected.
    Client Caching:
         - This stores the output cache in client's browser.
    Downstream:
         - This stores in any cache capable device that participates in page request.
    Web Server Caching:
         - This stores the output cache in the web server.
    None:
         - Output caching is deactivated.

Monday, June 20, 2011

ADO.Net Architecture

ADO.Net - Active X Data Object

What is ADO.Net ?
     It is a set of computer software components that programmers can use to access data and data services. It is commonly used by programmers to access and modify data stored in relational data base systems, though it can also access data in non-relational  sources.

Why ADO.Net?
   ADO.Net maintains a disconnected database access model, ie. when the application request for any for service, then the connection is opened to serve the request, and closed as soon as the request is completed. By keeping the connection opened only for a minimum time, it conserves system resources, and ensures maximum security and has less impact on system performance. When ADO.Net interacts with DB, it converts all the data into XML format for DB related operations and makes it more efficient.

ADO.Net data Architecture:
   Data access on ADO.Net requires 2 components : Dataset and Data Provider.

A connection object creates a connection to the data base. Command object enables the direct execution of the command to the database. When the command returns more than a single value, then it returns a data reader with data binded to it. Can also use data adapter to fill the data set.

ADO.Net Components:
    .Net Data Provider:
          Data provider components are explicitly designed for data manipulation and fast and forward only, read only access to data. The components of Data Provider are as follows:
                1. Connection: object provides connection to the database.
                2. Command: object enables access to database commands to return data, modify data, run stored procedure, send or retrieve parameter info.
                3. Data Reader: object provides high performance stream of data from the data source. Provides a forward only, read only connected record set.
                4. Data Adapter: provides bridge between data set and the data source.It populates a disconnected data set with data and performs update.
     Data Set:
            It is a disconnected in memory representation of data. Can be considered as a local copy of particular data.

Components of Data Provider:
    Connection Object:
             - It creates the connection to the database.
             - Provides 2 types of connection classes:
                      1. SqlConnection class : Designed specially for SQL Server.
                      2. OleDbConnection object: can provide connection to a wide range of servers like Access, Oracle.
            - Contains all the information regarding the connection to the data base.
   Command Object:
            - Represented by two commands SqlCommand and OleDbCommand
            - Used to execute the commands/ stored procedures to the data base. And returns data  back.
            - Provides 3 methods to execute the command
                     1. ExecuteNonQurery: Executes query which doesn't have any return value - Ex. Insert, Update, Delete
                     2. ExecuteScalar: Returns a single value from the database.
                     3. ExecuteReader: Returns a resultant set by the way of DataReader.
    DataReader Object:
           - Provides a forward - only, read - only, connected stream record set from the database.
           - Cannot be directly instantiated. Returned as the result of ExecuteReader method of Command object.
           - Provides rows of data directly to the application logic, when you do not need to keep in memory cache.
           - As only one row is in memory at a time, it provides lowest overhead in system performance, but requires exclusive use of open connection till the life time of the data reader.
     DataAdapter Object:
           - Core of ADO.Net disconnected data access.
           - middleman in communicating between the data set and the database.
           - Used to fill either the data set or the data table.
           - Can also be used to commit the changes.
           - Provides 4 properties:
                  SelectCommand, InsertCommand , DeleteCommand, UpdateCommand
           - When UpdateCommand is called, the respective changes are copied to the database and the appropriate InsertCommand, DeleteCommand, UpdateCommand are executed.