Monday, October 21, 2013

Session Object vs Singletons

I have a web application where I would like to pull user settings from a database and store them for Global access. Would it make more sense to store the data in a Singleton, or a Session object? What's the difference between the two?

Session object, definitely.

Singletons exist at the process level. This means that if you have 20 users accessing your site at any given moment, they're using the same singleton object. It's difficult to get used to this concept if you're not doing web development a lot.

Sessions exist at the user level. That means you can store data per user, not per process.

Ref:
http://stackoverflow.com/questions/2821941/session-vs-singleton-pattern

Thursday, October 3, 2013

What use of Is & as Operator?

"Is" operator

It is useful when we want to check whether one object belongs to particular class or not?. Have a look on below code using

class Test
{
public string Name { get; set; }
}
class Test1:Test
{
public string Surname { get; set; }
}
class Program
{
public static void Main(String[] args)
{
Test t = new Test();
if (t is Test1)
Console.WriteLine("t is object of Test1");
else
Console.WriteLine("t is not object of Test1");
Console.ReadLine();
}
}

"as" operator

as operator is useful when we want to convert one type to another type. But we have to keep in mind that both type should be compatible. Means by applying boxing and unboxing concept it should possible to convert one type to another type.
Let’s see by example.

class Test
{
public Test() { }
public string Name { get; set; }
} class Test1 : Test
{
public Test1() { }
public string Surname { get; set; }
}
class Program
{
public static void Main(String[] args)
{
Test1 t1 = new Test1();
t1.Name = "Mohan";
t1.Surname = "subramanian";
Test t = new Test();
t = t1 as Test;
Console.WriteLine("Name:-" + t.Name);
Console.ReadLine();
}
}

Here is sample output: Name:- Mohan

Friday, April 26, 2013

The fastest way to convert DataTable into JSON string

C Sharp:


public string GetJson(DataTable dt)
{

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

List> rows = new List>();

Dictionary row = null;

foreach (DataRow dr in dt.Rows)
{

row = new Dictionary();

foreach (DataColumn col in dt.Columns) {

row.Add(col.ColumnName, dr[col]);

}

rows.Add(row);

}

return serializer.Serialize(rows);

}


Vb.Net

Public Function GetJson(ByVal dt As DataTable) As String

    Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New      System.Web.Script.Serialization.JavaScriptSerializer()

Dim rows As New List(Of Dictionary(Of String, Object))

Dim row As Dictionary(Of String, Object)

For Each dr As DataRow In dt.Rows

row = New Dictionary(Of String, Object)

For Each col As DataColumn In dt.Columns

row.Add(col.ColumnName, dr(col))

Next

rows.Add(row)

Next

Return serializer.Serialize(rows)

End Function

Abstract Factory Vs Factory Method

Abstract Factory Provides an interface for creating families of related or dependent objects without specifying their concrete classes ...