This may make me sound like I am antiquated and behind the times, and in this aspect you are probably right. Since I started messing with programming quite a few years ago, I’ve tried to make the programs I write as user friendly and as easy to maintain as possible. Recently I have adopted a few things I’ve picked up from others like making the methods in my programs accessible through web services when it has merit and things of the sort.
One of the things I have always done is provide extensive config files in XML format in order to alleviate having to modify the code when simple things change such as passwords, database names, host addresses etc. This has in my opinion always been a great venue to ensure maintainability and stability. But the big issue always was XML..
XML (Extensible Markup Language) is a general-purpose specification for creating custom markup languages.[1] It is classified as an extensible language, because it allows the user to define the mark-up elements. XML’s purpose is to aid information systems in sharing structured data, especially via the Internet, [2] to encode documents, and to serialize data; in the last context, it compares with text-based serialization languages such as JSON, YAML and S-Expressions. [3]
more… The problem I’ve always had with XML is parsing the file; although the support for it and the built in tools have improved greatly over the past few years it still requires considerable amount of code in order to gather the data in some languages (C#), and another considerable amount of code in order to update or change the data in the file. When I was learning Object Oriented programming a while back we were introduced to the concept of Serialization
In computer science, in the context of data storage and transmission, serialization is the process of converting an object into a sequence of bits so that it can be stored on a storage medium (such as a file, or a memory buffer) or transmitted across a network connection link. When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward.
more… But like the Wikipedia definition above says “this process is not straightforward”, you used to have to define Interfaces and formats and if your object had any kind of list or array in it, you couldn’t use it unless you wanted to spend a considerable amount of time making it work.
While I was working on the practice projects I grew to hate Serialization and had not used it since. Yesterday in a fit of rage, because my XML config file was being stubborn I stumbled across an implementation that used a Serialized object for storing configuration. I was baffled by the ease in which this was accomplished and I immediately implemented it. Now it its extremely easy to add and remove config parameters from my program and at the same time I don’t have to worry about someone deleting or changing the config file since it is stored in binary and gets re-created if deleted.
In order to serialize an object your class must be declared as [Serializable] . After you have done this, the below example will take care of the serialization for you.
//Serialize an Object
FileStreamfs = new FileStream(@”prog.conf”, FileMode.Create); //Create new File where the object will live
BinaryFormatter bf = new BinaryFormatter(); //Create new BinaryFormatter to Serilize Object
bf.Serialize(fs, YOUROBJECT); //Serialize Object
fs.Close(); //Close File
//Deserialize
FileStreamfs = newFileStream(@”prog.conf”,FileMode.Open); //Open File Containing Serialized Object
YOUROBJECT obj = (YOUROBJECT) (new BinaryFormatter().Deserialize(fs));
fs.Close(); //Close File
Remember to include the System.IO and System.Runtime.Serialization.Formatters.Binary libraries.
Originally published at
josecgomez.com. You can comment here or
there.