XML Serialization of Content objects

An Item may contain a Content Object. The content of the item is an optional object containing extra information. The true power of NCommet is that ANY class can be placed as a content of an item as long as it fulfills the following two requirements:
  • It inherits ContentBase, which is the base class that defines the content of an Item.
  • It is serializable.
For example the following class that describes a Post could be placed as the content of an item because it fulfills the above two requirements:


The question is how does NCommet handle this capability that any class can become a content of an item. The answer is through XML serialization. Before the item is persisted in the database, NComment serializes the Content Object and places the XML Output in the corresponding column in the database. It also places information for the type of the object that is serialized in another column, so that when the item is loaded from the database, NCommet knows how to handle its content (i.e. deserialize the serialized content object and cast it to the appropriate class).

What are the benefits of this technique?

First of all, it gives the user the capability to only worry that the content class he creates only fullfills the two requirements we mentioned. He doesn’t have to care if his content class contains 1, 2 or any number of fields. He only has to care that these fields are serializable. After all, the database only contains the XML Representation of the object so the number of fields doesn’t matter.

In addition, the user may add or remove fields from the content classes he creates freely. Again, nothing else needs to change in his code since these fields are not represented as separate columns in the database. The only thing that changes is the XML Representation of the object, which now contains the new fields, which are either more or less.

Finally, it allows the user to construct a separate layer in his application, that decouples the items from their contents. This layer represents the business model of the application and all the content classes are placed in there.

Last edited Apr 10, 2008 at 11:38 AM by NikolaosGeorgiou, version 5


No comments yet.