Thursday, October 29, 2015

Persistence concept in Objects of C#

 What is Persistence concept?

Persistence is the capability of an object to save and restore data. The object can be saved to the disk with its current state. You can use it later from the disk, with the attributes with which it was saved. Without this capability of saving and restoring we would have to re-configure object every time we quit an application.

How Can you do it Programmatically in objects of C# and your own Objects ?

The .NET framework ships with two serialization systems. One is represented by the System.Xml.Serialization.XmlSerializer class and is intended for saving and loading objects to and from human-readable XML. The other is represented by System.Runtime.Serialization.Formatter, and is designed to save and load arbitrary objects in binary or SOAP format (or any other custom formats you may want to provide, but you probably won't need that), mainly for remoting, but useful for saving and loading to and from files, as well. Both are useful in different situations -- if you have a relatively simple object and want to have a lot of control over the format in which it's saved, use XmlSerializer. If you have a complex object (especially if it has recursive object references -- that is, a property that points to an object that has a back-reference to the parent object), and don't really care what format you use, use Formatter (specifically, BinaryFormatter, as it's more concise than SoapFormatter). The Formatter classes can serialize and deserialize any object that has a SerializableAttribute applied to it, or implements ISerializable. 99 out of 100 times, you won't need special support for ISerializable -- just put SerializableAttribute on your class, and it'll usually work (the exception being if your object has members that can't be persisted properly, such as GDI+ objects like Brush or Pen -- if you need this support, post back with some details, and I can give you examples of how to do it). Here's an example of how to use BinaryFormatter (System.Runtime.Serialization.Formatters.Binary.BinaryFormatter) to save and load an object:
public void SerializeObject(object o, string fileName)
{
 using (Stream s = File.OpenWrite(fileName))
 {
  BinaryFormatter bf = new BinaryFormatter();
  bf.Serialize(s, o);
 }
}


public object DeserializeObject(string fileName)
{
 using (Stream s = File.OpenRead(fileName))
 {
  BinaryFormatter bf = new BinaryFormatter();
  return bf.Deserialize(s);
 }
}
XmlSerializer is a bit less robust, in that it is more limited in the objects it's able to handle (for example, it can't handle recursive references or, in fact, any kind of strongly-typed reference to another object in the graph other than a simple parent/child relationship), but it's useful when you need your data to be human-readable (or human-editable), and even more so if you have an already-established XML format that you have to use. If you have an XSD schema for your format, you can use the xsd.exe tool included in the .NET framework to generate a class which the XmlSerializer can use to read and write the XML described in the schema. Here are the same two functions described above, implemented using the XmlSerializer:
public void SerializeObject(object o, string fileName)
{
 using (TextWriter tw = new StreamWriter(File.OpenWrite(fileName)))
 {
  XmlSerializer xs = new XmlSerializer(o.GetType());
  xs.Serialize(tw, o);
 }
}

public object DeserializeObject(string fileName, Type t)
{
 using (TextReader tr = new StreamReader(File.OpenRead(fileName)))
 {
  XmlSerializer xs = new XmlSerializer(t);
  return xs.Deserialize(tr);
 }
}
Notice that the major differences in usage between the two serializers are that Formatter uses Streams while XmlSerializer can use readers and writers (or streams, but it simply creates a reader/writer to wrap around the stream internally), and that Formatter can get all the information it needs from the serialized stream, but XmlSerializer needs to be told the type of the root object.

No comments:

Post a Comment