Let me start with an interesting story. In computer science, coupling or dependency is the degree to which each program module relies on each one of the other modules. The more it is tightly coupled, the more your implementation becomes concrete. Also keep this in mind that strong coupling or tight coupling cannot be zerod down completely. So in order to make any application easy to maintain as well as easy to understand and extend, we need to adhere to design principles.
Core working of Garbage Collection
October 16th, 2010 by Suprodeep No comments »Garbage Collection is a true magical implementation. I can try to show with some in-depth facts.
My Definition
Garbage Collector is a best example of daemon thread, a thread which runs in the background all the time. I know you must be asking can I make a thread similar like this !! Yes you can.
Thread.IsBackground = True;
» Read more: Core working of Garbage Collection
IEnumerable and IEnumerator Usages..When and Why
October 8th, 2010 by Suprodeep No comments »Before continuing, let me explain the structure, members of the IEnumerable, IEnumerator interfaces. The IEnumerable interface contains an abstract member function called GetEnumerator() and return an interface IEnumerator on any success call.
This IEnumerator interface will allow us to iterate through any custom collection. IEnumerator interface is meant to be used as accessors and is not helpful to make any changes in the collection or elements of the collection.
» Read more: IEnumerable and IEnumerator Usages..When and Why
Application/Data Caching
October 3rd, 2010 by Suprodeep 3 comments »The Application or data caching is implemented when you want your pages to cache the entire page for a certain duration of time. This is one of the method of Caching. Below example would give you a basic implementation process of Application Caching.
Html/Aspx Code:
<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”Default.aspx.cs” Inherits=”CachingImplementation._Default”%>
<%@ Import Namespace=”System” %>
<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.SqlClient” %>
<%@ Import Namespace=”System.Configuration” %>
<%@ Import Namespace=”System.Web” %>
<%@ Import Namespace=”System.Collections” %>
<%@ Import Namespace=”System.IO” %>
<!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 runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:DataList ID=”dlUsers” Style=”z-index: 101; left: 44px; position: absolute; top: 104px”
runat=”server” Height=”148px” Width=”343px” BorderWidth=”1px” GridLines=”Horizontal”
CellPadding=”4″ BackColor=”White” ForeColor=”Black” BorderStyle=”None” BorderColor=”#CCCCCC”>
<SelectedItemStyle Font-Bold=”True” ForeColor=”White” BackColor=”#CC3333″></SelectedItemStyle>
<FooterStyle ForeColor=”Black” BackColor=”#CCCC99″></FooterStyle>
<HeaderStyle Font-Bold=”True” ForeColor=”White” BackColor=”#333333″></HeaderStyle>
<ItemTemplate>
<table>
<tr>
<td>
<%#DataBinder.Eval(Container.DataItem, “EmpID”)%>
</td>
<td>
<%#DataBinder.Eval(Container.DataItem, “FName”)%>
</td>
<td>
<%#DataBinder.Eval(Container.DataItem, “Lname”)%>
</td>
<td>
<%#DataBinder.Eval(Container.DataItem, “Salary”)%>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
<div>
<asp:Label ID=”lblChange” runat=”server” Text=”Nothing”></asp:Label>
</div>
</form>
</body>
</html>
» Read more: Application/Data Caching
Shallow Copy Vs Deep Copy
October 2nd, 2010 by Suprodeep 2 comments »Shallow Copy: The value type objects/variables do not reflect the changes done to either of the objects, as they are stored in Stack.The core difference here is the reference type variables/objects will reflect the changes done to both the objects copied.
Deep Copy: The reference object/variables do not reflect the changes if any of the object is changed. This is basically a seprate entity of the same object which can only be possible when a copy of internal semantics of done of an object but it is made to point to a separate memory location. This is to be implemented by the application user and can be done only by implmeneting ICloneable. So ICloneable can be used to implement both shallow as well as deep copy. It is on the discretion of what your application is requiring.
Array.Clone/List.Clone/MemberwiseClone all create a shallow copy. Do not get confused with this.
Sample Code:
/// <summary>
/// For Deep copying of the reference type variable/objects we will create a new instance and then copy over the data into it.
/// </summary>
[Serializable]
public class MyBaseClass:ICloneable
{
public static string CompanyName = “My Company”;
public int age;
public string name;
//Reference variables/Objects
//List<string> lst = new List<string>(){“a”,”b”};
public string[] arr = new string[]{“Test1″,”Test2″};
#region Implementation of ICloneable: Using Serialization
public object Clone()
{
//——————————————————–(1) Using Serialization—————————————–
//var ms = new MemoryStream();
//var bf = new BinaryFormatter();
//bf.Serialize(ms, this);
//ms.Position = 0;
//object obj = bf.Deserialize(ms);
//ms.Close();
//return obj;
//——————————————————————-(2) Without using Serialization———————
MyBaseClass obj = this.MemberwiseClone() as MyBaseClass;
obj.arr = new string[arr.Length];
obj.arr = (string[])this.arr.Clone();
return obj;
}
IS and AS Operator
August 29th, 2009 by Suprodeep No comments »A way of casting in C# language is to use the IS operator. The is operator checks
whether an object is compatible with a given type, and the result of the evaluation is a
Boolean: true or false. The is operator will never throw an exception. The following code
demonstrates:
Object o = new Object();
Boolean b1 = (o is Object); // b1 is true.
Boolean b2 = (o is Employee); // b2 is false.
If the object reference is null, the is operator always returns false because there is no object
available to check its type.
The is operator is typically used as follows:
if (o is Employee) {
Employee e = (Employee) o;
// use e within the remainder of the ‘if ‘ statement.
}
In this code, the CLR is actually checking the object’s type twice. The is operator first checks
to see if o is compatible with the Employee type. If it is, inside the if statement, the CLR again
verifies that o refers to an Employee when performing the cast. The CLR’s type checking
improves security, but it certainly comes at a performance cost, because the CLR must determine
the actual type of the object referred to by the variable (o), and then the CLR must walk the
inheritance hierarchy, checking each base type against the specified type (Employee).
C# offers a way to simplify this code and improve
its performance by providing an AS operator:
Employee e = o as Employee;
if (e != null) {
// Use e within the ‘if’ statement.
}
In this code, the CLR checks if o is compatible with the Employee type, and if it is, as returns
a non-null reference to the same object. If o is not compatible with the Employee type, the as
operator returns null. Notice that the as operator causes the CLR to verify an object’s type
just once. The if statement simply checks whether e is null; this check can be performed
faster than verifying an object’s type.
The as operator works just as casting does except that the as operator will never throw
an exception. Instead, if the object can’t be cast, the result is null. You’ll want to check to
see whether the resulting reference is null, or attempting to use the resulting reference will
cause a System.NullReferenceException to be thrown. The following code demonstrates:
Object o = new Object(); // Creates a new Object object
Employee e = o as Employee; // Casts o to an Employee
// The cast above fails: no exception is thrown, but e is set to null.
e.ToString(); // Accessing e throws a NullReferenceException
Introducing JSON
August 5th, 2009 by Suprodeep 7 comments »JSON is JavaScript Object Notation. JSON is a safe and reliable data interchange format in JavaScript. This format is easy for humans to read and machines to understand.
AJAX uses JSON format to communicate between client and server.
JSON is mainly used in web applications AJAX enabled. These applications force their server-side code, which can be either in PHP, JSP, ASP.NET, etc to format the data that they must send back to the requesting client application in a JSON format
SDLC Models
August 2nd, 2009 by Suprodeep 11 comments »V-Shaped Model
Just like the waterfall model, the V-Shaped life cycle is a sequential path of execution of processes. Each phase must be completed before the next phase begins. Testing is emphasized in this model more so than the waterfall model though. The testing procedures are developed early in the life cycle before any coding is done, during each of the phases preceding implementation.
Requirements begin the life cycle model just like the waterfall model. Before development is started, a system test plan is created. The test plan focuses on meeting the functionality specified in the requirements gathering.
The high-level design phase focuses on system architecture and design. An integration test plan is created in this phase as well in order to test the pieces of the software systems ability to work together.
Implementation of Asynchronous Page Processing
August 1st, 2009 by Suprodeep 6 comments »Each request is picked from ASP.NET thread pool. The request handling is taken care by HttpHandler. The HttpHandler internally implements IhttpHandler interface. And in order to implement this interface we implement ProcessRequest method. The thread calls this method and waits until its termination. If the page response is slow or unreponsive it will wait till the time it doesn’t get the response, thus making you webpage slow.. HttpHandlers can be synch or Asynch. By Default they are synchronous.
Lets practice Defensive Programming
July 23rd, 2009 by Suprodeep No comments »I came across an article which explains some of the .NET Coding standards. One of of them is to practice Defensive Programming.
What is Defensive Programming ?
Stop Exceptions BEFORE They Happen!
Any code that might cause an exception (accessing files, using objects like DataSets etc.) should do checking before operating on that object so an Exception is not thrown.
For example
Call File.Exists to avoid a FileNotFoundException
Check and object for null
Check a DataSet for rows
Check an Array for bounds
Check String for null or empty
Reference : Code Camp 2009