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

Abstract Factory Vs Factory Method

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