Sunday, January 9, 2011

Remoting & Webservice

Process:
1. It is an instance of the computer program that is being excuted.
2. It is used as a secuirty boundaries.
3. One process has its own virtual memory and does not overlap the other process virtual memory. due to this one process cannot crash the other process.
4. Any Problem or error in one process does not affect the other process.

Application Domain:
1. It is used as a security boundary in which .Net application executes.
2. In application domain multiple applications can run in same process without influencing each other.
3. Any problem or error in one application domain does not affect the other applicaiton domains.
4. One Process can have multiple applicaiton domains.

Note: To invoke method in a object running in different applicaiton domain .Net remoting is used.

What are two different types of remote object creation mode in .Net?
There are two different ways in which object can be created using Remoting.
1. SAO (Server activated objects) also called as Well-Known call mode.
2. CAO (Client activated objects)

1. SAO: It has two modes "Single Call" & "Singleton".
a. Single call: In it object is created with every method call thus making the object stateless.
b. Singleton: In it object is created only once and the object is shared with all clients.

2. CAO: These are stateful as compared to SAO. In it the creation request is sent from client side. Client holds a proxy to the server object created on server.

Monday, January 3, 2011

Constructors in C#

A constructor is a special method or function used to initialize & create the object of a class.

Types Of Constructory:
Constructor can be of two types
1. Class Constructor (Static Constructors)
2. Instance Constructor (Non Static Constructors)


1. Class Constructor (Static Constructor): A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are
referenced.

Static constructors have the following properties:
1. A static constructor does not take access modifiers or have parameters.
2. A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
3. A static constructor cannot be called directly.
4.The user has no control on when the static constructor is executed in the program.
5. A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
6. Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.


2. Instance Constructor (Non-static constructor): These constructors are normally used to initialize the mermbers and to create the constructor of class.

1. Public
A class constructor is by default public.

2. Private
When we create a private constructor, we cannot create object of the class directly from a client. Also we cannot inherit this class to another class.So by this way we can add security to class that it cannot be instantiated. Also a common use for a private constructor is to implement the singleton pattern.

3. Protected
When we declare a constructor protected then only drive class can create instance of that class.


Copy Constructor:
A copy constructor is a special constructor used to create a new object as a copy of an existing object. This constructor takes a single argument: a reference to the object to be copied. Unlike some languages, C# does not provide a copy constructor. If you create a new object and want to copy the values from an existing object, you have to write the appropriate method yourself.


class Person
{
private string name;
private int age;

// Copy constructor.
public Person(Person previousPerson)
{
name = previousPerson.name;
age = previousPerson.age;
}

// Instance constructor.
public Person(string name, int age)
{
this.name = name;
this.age = age;
}

// Get accessor.
public string Details
{
get
{
return name + " is " + age.ToString();
}
}
}

class TestPerson
{
static void Main()
{
// Create a new person object.
Person person1 = new Person("George", 40);

// Create another new object, copying person1.
Person person2 = new Person(person1);
System.Console.WriteLine(person2.Details);
}
}


Shallow Copy Vs Deep Copy:
Shallow copy copies the structure of the object, deep copy copies structure as well as data.

Dataset.Clone()- Shallow Copy
Dataset.Copy() -- Deep Copy