• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

.NET Serialization: saving your object in a database... is this better than saving data in columns?

Hi, I have a question... possibly a stupid question, I'm not sure.

But, just started dabbling with serialization because I've created a website which savesinformation related to the user. So, in order to work with this data, I extract the user's information from the database, put it into my User object and then perform manipulations.

But, ultimately the data is stored in a standard table in an oracle database. So, of course I've just realised and managed to get working, the process of saving that data in the oracle table (BLOB field) as a serialized object. Then I can extract that serialized object directly without having to extract the information column by column.

So... knowing little about this and not really being able to see the possible difficulties down the road... I am just wondering to myself... why bother storing the data in standard format at all? Why not just do away with all the columns and only store the User object?

So why not? Well, I guess the obvious answer is that you can't run simple queries on your database anymore... and you're going to want to do that. But, I am just wondering whether I'm better off, not only storing the data in a standard column, but then including a User column at the end where I store my User object also. Then I can more or less build my code around extracting that object when I want to deal with a user's information... instead of extracting every column and building my Task object column by column in code.

What do the experts say?! What's the standard way people do these types of things?
3 Solutions
Ivo StoykovCommented:
Well, depends - how big is your application, how many users you;ll store, how often you;ll need to search in... you name it.

Technically it is possible. But from architecture point of view - depends on many things.


Ivo Stoykov
AidenAAuthor Commented:
what do you mean technically it's possible? I just tested and was able to store a user object in a blob along with all the other data and then extract it again. So... it's possible yes. But do companies generally do something like this... and if not, then why not?

There is a bit of overhead with serialization, so that might be a reason. But binary formatter is very fast, and I don't think it would affect things in my case. There will only be about 100 users using the site and not in with heavy usage either
Ivo StoykovCommented:
technically it's possible means
> ... able to store a user object in a blob along with all the other data and then extract it again.

> do companies generally do something like this... and if not, then why not?
Decision is made upon many considerations. You cannot just ask "to store or not to store?"
These are fundamental questions about database storage. Each decision has pro and cons and this is why some companies do other - don't.

The best answer for me is - test it, measure it and you'll get the answer.


Ivo Stoykov
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

AidenAAuthor Commented:
Yeah but that's not really a useful answer if you don't mind me saying so :(

Here is an answer... just saw this article


So based on that, I'd be inclined to say that it is a bad idea to store your objects in a serialized way. (1) As I mentioned, you can't run queries easily on it anymore (2) As the article mentions, if you need to make a small change to the data, this now requires a large change as the entire blob has to be updated... so there is loss of functionality here and loss of performance (although perhaps easier to code with).

If you were to include a blob with your object along with all the other data then in fact now it's worse because you'd have to make two updates if you wanted to change the data, one to the columns with the data and then to the blob with the object in it. So, that makes no sense (thinking about it now).

Where it makes sense is probably where I was thinking of using it in the first place, in fact. I have an ERROR table which contains some error information, and I wanted to include all the user information I have in a column along with the error information so I can have a better idea about what caused the error. In that case, I don't need to update the blobs, I don't need to run queries on the blobs... I just need to extract the information associated with the error as is. So, that's probably a good situation for it.

So, I don't know... that seems like a clearer analysis? Unless there's something wrong with the above...

Whether to persist User object into DB depends on how costly you think is it to re-create the same object again?

In both cases Database trip is still there. If we consider volume of data transferred between the two approaches then I believe serialized blob of User object would be larger.

If re-creation of User object is expensive then how about storing it into Cache. Thus you can put logic that retrieves ready-made object from Cache and if required User object is not cached create it and put into Cache.
slightwv (䄆 Netminder) Commented:
My 2 cents:  

I would store it unserialized for many of the reasons you posted:  Ease of update and selectivity.

You also do not need to write a bunch of maintenance/migration code if the serialized object changes.

The 'R' in RDBMS is Relational.  If you are not going to take advantage of the power of a database, then why use one?  Let alone Oracle.  There are MUCH cheaper options at that point.
AidenAAuthor Commented:
yeah thanks for that... i'll go with what i was going for initially then, just saving the object in a blob if all i want to do is store it and read it later as I do with the error table... in that situation it's quite useful...

thanks for input...

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now