Ram
Ram

Reputation: 11644

Storing objects in the database

I am using SQL Server 2008 with NHibernate for an application. In the application I need to create multiple object of a Info class and use it in multiple places. I also need to store that object in the databse.

There are multiple types of Info class.

To store these objects of Info class I have two options

What is the advantage of storing the serialized object in the database over storing its values as multiple strings?

-Ram

Upvotes: 0

Views: 2122

Answers (4)

Pongsathon.keng
Pongsathon.keng

Reputation: 1435

Serialized obejct (XML) If you store the class as XML. You will be able to search the contect of the class by using Xquery. This way is eay way if you want to search(with or without conditions). More over, you can create index over XML column. The XML index will make you application faster.

AS string If you don have bussines login to look at the content of class.

Upvotes: 0

StuartLC
StuartLC

Reputation: 107247

The most common issue with storing an object as a serialized stream is that it is difficult to search the properties of the object once it is stored, whereas if each property of the object is explicitly stored in its own strongly typed column, it can be indexed for searches, and you get the integrity benefit of strongly typed storage.

However, At least if the object is XmlSerialized into an XML column in SQL, you can use technologies such as xquery and OPENXML to ease your searches.

Upvotes: 0

grizzly
grizzly

Reputation: 1156

If you store the serialized object into the db:

  • You don't have to rebuild it from the partial data (ie. write your own deserializer if the behaviour is default, create objects from the partial data)
  • You must create the object "manually"
  • May be faster in some cases
  • Stores redundant infrastructure data
  • You may choose multiple formats (XML, custom format, blobs)
  • You have fully prepared serialized objects that are ready to be processed anywhere (sent over the network, stored in a disk)

I you store the multiple strings, you:

  • Need to build the objects "manually"
  • May use the database data in different scenarios (from .net, to build another structures such as cubes)
  • The data is much more compact
  • May store the data in a relational normalized form which is (almost) always a good practice
  • Query the data
  • And the overall more versatile usage of the data.

I would definitely go for the relational normalized form to store the strings and then build the corresponding class builder in .net.

Upvotes: 3

Davide Piras
Davide Piras

Reputation: 44605

I would definitely store records and fields and not just a chunk of bytes ( either binary or text or xml ) representing the current status of your object.

it depends of course on the complexity of your business entities ( in your case the Info class ), but I would really avoid saving the serialized version of it in 1 column.

if you explode all properties into fields you can query better for records having certain values and you can handle new columns and releases much easier.

Upvotes: 0

Related Questions