Archive

Archive for the ‘php’ Category

Mysql basics

February 27, 2010 Leave a comment

Hello again

this post is for new user who are new to mysql, in this post i explained how to create new database and use it to perform operations on tables.

to create new database type

create database ‘database_name’;

and to Create tables in this database, first we need to select the database by typing

use database_name;

and then type query for table creation

create table table_name(field 1,field 2,…..,field n );

Advertisements
Categories: mySQL

Serialization in .NET

January 26, 2010 Leave a comment

Serialization is the process of saving the state of an object in a persistent storage media by converting the object to a linear stream of bytes.  The object can be persisted to a file, a database or even in the memory.  The reverse process of serialization is known as de-serialization and enables us to re-construct the object from the previously serialized instance of the same in the persistent or non-persistent storage media.

Serialization in .NET is provided by the System.Runtime.Serialization name space.  This name space contains an interface called IFormatter which in turn contains the methods Serialize and De-serialize that can be used to save and load data to and from a stream.  In order to implement serialization in .NET, we basically require a stream and a for matter.  While the stream acts as a container for the serialized object(s), the for matter is used to serialize these objects onto the stream.

The basic advantage of serialization is the ability of an object to be serialized into a persistent or a non-persistent storage media and then reconstructing the same object if required at a later point of time by de-serializing the object.  Remoting and Web Services depend heavily on Serialization and De-serialization.  Refer to the figure A.

Figure A
Serialization

The above figure illustrates that the serialized object is independent of the storage media, i.e., it can be a database, a file or even the main memory of the system.

Types of serialization

The .NET Framework provides certain built-in mechanisms which can be used to serialize and deserialize objects. This functionality can be found in the System.Runtime.Serialization and the System.Xml.Serialization namespaces of the .NET Framework.

Serialization in .NET can be classified into four types as shown in Figure B:

Figure B

Binary Serialization

Binary serialization is a mechanism which writes the data to the output stream such that it can be used to re-construct the object automatically.  The term binary in its name implies that the necessary information that is required to create an exact binary copy of the object is saved onto the storage media.  A notable difference between Binary serialization and XML serialization is that Binary serialization preserves instance identity while XML serialization does not.  In other words, in Binary serialization the entire object state is saved while in XML serialization only some of the object data is saved.  Binary serialization can handle graphs with multiple references to the same object; XML serialization will turn each reference into a reference to a unique object.  The following code listing shows how we can implement binary serialization.

Listing 1:

public void BinarySerialize(string filename, Employee emp)

{   FileStream fileStreamObject;

try

{     fileStreamObject = new FileStream(filename, FileMode.Create);

BinaryFormatter binaryFormatter = new BinaryFormatter();

binaryFormatter.Serialize(fileStreamObject, emp);   }

finally   {     fileStreamObject.Close();   } }

The following code listing shows how we can implement binary de-serialization.

Listing 2:

public static object BinaryDeserialize(string filename)

{   FileStream fileStreamObject;

try   {     fileStreamObject = new FileStream(filename, FileMode.Open);

BinaryFormatter binaryFormatter = new BinaryFormatter();     return (binaryFormatter.Deserialize(fileStreamObject));   }

finally   {     fileStreamObject.Close();   } }

SOAP Serialization

The SOAP protocol is ideal for communicating between applications that use heterogeneous architectures.  
In order to use SOAP serialization in .NET we have to add a reference to
System.Runtime.Serialization.Formatters.Soap in the application.
 The basic advantage of SOAP serialization is portability.
 The SoapFormatter serializes objects into SOAP messages or parses SOAP messages and
extracts serialized objects from the message.  The following code listing shows how we
can implement serialization using the SOAP protocol.

Listing 3:

public void SOAPSerialize(string filename,Employee employeeObject)
{
  FileStream fileStreamObject = new FileStream(filename, FileMode.Create);
  SoapFormatter soapFormatter = new SoapFormatter();
  soapFormatter.Serialize(fileStreamObject, employeeObject);
  fileStreamObject.Close();
}

The following code listing shows how we can implement de-serialization using the SOAP protocol.

Listing 4:
public static object SOAPDeserialize(string filename)
{
  FileStream fileStreamObject = new FileStream(filename, FileMode.Open);
  SoapFormatter soapFormatter = new SoapFormatter();
  object obj = (object)soapFormatter.Deserialize(fileStreamObject);
  fileStreamObject.Close();
  return obj;
}

XML Serialization

According to MSDN, "XML serialization converts (serializes) the public fields and properties
of an object or the parameters and returns values of methods, into an XML stream that conforms
 to a specific XML Schema definition language (XSD) document. XML serialization results in
strongly typed classes with public properties and fields that are converted to a serial
format (in this case, XML) for storage or transport. Because XML is an open standard,
the XML stream can be processed by any application, as needed, regardless of platform."
Implementing XML Serialization in .Net is quite simple.  The basic class that we need to
 use is the XmlSerializer for both serialization and de-serialization.  
The Web Services use the SOAP protocol for communication and the return types and the
 parameters are all serialized using the XmlSerializer class.  XML Serialization is however,
much slower compared to Binary serialization.  We can set a property as an XML attribute as
shown in the code listing below.

Listing 5:
[XmlAttribute("empName")]
public string EmpName
{
  get
  {
    return empName;
  }
  set
  {
    empName = value;
  }
}

The following code listing shows how we can implement XML serialization.

Listing 6:

public void XMLSerialize(Employee emp, String filename)
{
  XmlSerializer serializer = null;
  FileStream stream = null;
  try
  {
    serializer = new XmlSerializer(typeof(Employee));
    stream = new FileStream(filename, FileMode.Create, FileAccess.Write);
    serializer.Serialize(stream, emp);
  }
  finally
  {
    if (stream != null)
      stream.Close();
  }
}

The following code listing shows how we can implement XML de-serialization.

Listing 7:

public static Employee XMLDeserialize(String filename)
{
  XmlSerializer serializer = null;
  FileStream stream = null;
  Employee emp = new Employee();
  try
  {
    serializer = new XmlSerializer(typeof(Employee));
    stream = new FileStream(filename, FileMode.Open);
    emp = (Employee)serializer.Deserialize(stream);
  }
  finally
  {
    if (stream != null)
      stream.Close();
  }
  return emp;
}

Custom Serialization

In some cases, the default serialization techniques provided by .NET may not be sufficient in real life.
 This is when we require implementing custom serialization.  
It is possible to implement custom serialization in .NET by implementing the
 ISerializable interface.  This interface allows an object to take control of
its own serialization and de-serialization process.  It gives us a great deal of flexibility in
the way we can save and restore objects.The ISerializable interface consists of a single method,
GetObjectData, which accepts two parameters.

The SerializationInfo class serves as the container for all the data we want to serialize.  
The AddValue method is called to add the objects we want to serialize to this container.
 The implementing class needs to have the GetObjectData method and a special constructor which is
 used by the common language runtime during the process of de-serialization.
 The following code listing shows how we can implement Custom Serialization.

Listing 8:
public class Employee: ISerializable
{
  private int empCode;
  private string empName;
  protected Employee(SerializationInfo serializationInfo, StreamingContext
    streamingContext)
  {
    this.empCode = serializationInfo.GetInt32("empCode");
    this.empName = serializationInfo.GetString("empName");
  }
  public void ISerializable.GetObjectData(SerializationInfo serializationInfo,
    StreamingContext streamingContext)
  {
    serializationInfo.AddValue("empCode", this.empCode);
    serializationInfo.AddValue("empName", this.empName);
  }
}

The following listing shows how we can implement Custom Serialization on a Custom Collection

class that extends the CollectionBase class of the System.Collections namespace.

Listing 9:

[Serializable]
public class EmployeeCollection: System.Collections.CollectionBase,
  ISerializable
{
  private int empCode;

  public EmployeeCollection()
  {
    empCode = 1;
  }

  protected EmployeeCollection(SerializationInfo info, StreamingContext context)
    : base(info, context)
  {
    empCode = info.GetInt32("empCode");
  }

  public virtual void GetObjectData(SerializationInfo info, StreamingContext
    context)
  {
    base.GetObjectData(info, context);
    info.AddValue("empCode", empCode);
  }
}

Advantages and Disadvantages of Serialization

The following are the basic advantages of serialization:
  • Facilitate the transportation of an object through a network
  • Create a clone of an object
The primary disadvantage of serialization can be attributed to the resource overhead (both the CPU and the IO devices) that is involved in serializing and de-serializing the  data and the latency issues that are involved for transmitting the data over the network.   Further, serialization is quite slow.  Moreover, XML serialization is insecure, consumes  a lot of space on the disk and it works on public members and public classes and not on the private or internal classes.  Therefore, it compels the developer to allow the class to be accessed to  the outside world.
Categories: php, Uncategorized