Saving data into DB s considered Serialization? - c#

Serialization -> Convert an object to a binary representation that can be then written to Disk or write on a file..
Above is the basic definition of serialization that I know. But what does this really mean? I have a class in my application and I use this to get data from user and store it in Database. Does this mean I am using serializion here? Even storing the data is more like saving the state of the object, I can get the data and form the same object once again.
Can any one light me up with whats a real serialization? If serialization is not used what will be the result? Whats the difference between saving the data in a file and doing the serialization (to save the data) in a file.

I doubt storing data in a database should be considered serialization. Even when you're storing the data coming from your object-oriented programming layer, actually you're translating objects into the relational world and viceversa. This is called data-mapping.
Perhaps you may argue performing an INSERT is storing data in an interoperable format. Not necessarily, since SQL is a domain-specific language to manage relational data, and you don't know how the data is actually stored either in memory or disk. SQL itself isn't a serialization format.
Since most databases are on disk, you can consider serialization the process of persisting database registers to disk in order to retrieve or alter them afterwards, and use RAM to optimize reads and writes without carrying the entire database to memory.
In the other hand, serialization can be done in binary or non-binary formats. For example, you can serialize an object into JSON, and JSON isn't a binary format. Also, XML it has been used as serialization format for years and it's not binary.
A good definition to serialization may be: consider serialization when some in-memory object is turned into an interoperable representation that can be stored in disk or transmitted over the wire to easily get back it as in-memory object in any platform and language being capable of understanding the serialization format.
Examples:
A REST API sending a list of users as data-transfer objects serialized to JSON.
An application lets user visually edit its configuration and settings. When UI needs to show current values, it will deserialize the configuration back to objects to bind them to the UI, and once the user presses Save, configuration gets serialized again to disk.
An application provides its own backup. The backup can be the entire object graph serialized as JSON.

Related

SQL Server: variable length XML storage

I'm in the process of writing an application that interacts with a third party application.
The third party application will be passing my application several raw XML requests. I would like to save each of these requests in a communications log in my DB.
What's the most efficient way to store this variable-length data? VARCHAR(MAX)? NVARCHAR(MAX)?
If one is a better choice than the other (or there is another option I'm missing), please explain why it's the best choice.
Since you're using SQL Server 2K5 the best data type to store XML data is xml.
This provides parsing and schema validation features. It also allows you to index the XML data later if need be.
XML seams to be the obvious data type of choice when dealing with XML but not always.
Have a look at this article by Robert Sheldon. Working with the XML Data Type in SQL
In some cases, you shouldn’t use the XML data type, but instead use large object storage—VARCHAR(MAX), NVARCHAR(MAX), or VARBINARY(MAX). For example, if you simply store your XML documents in the database and retrieve and update those documents as a whole—that is, if you never need to query or modify the individual XML components—you should consider using one of the large object data types. The same goes for XML files that you want to preserve in their original form, such as legal documents. If you need to retain an exact textual copy, use large object storage.
I have used the XML Datatype for this type of thing MSDN link - XML DataType 2005
Native and allows you to do some normal angle bracket things to the actual data.
Big plus is that I am not converting or messing around with the actual data, and introducing subtle bugs with the actual XML.
Big plus if you want to do anything with the XML like render it.
Downside is that you have to be aware the column is XML data and you need to code for it in upstream apps.
Adding to Yuck's answer:
VARCHAR(MAX)
XML means Unicode, and if you choose non-Unicode storage then data loss is almost certain
NVARCHAR(MAX)
appropriate if you only want to log the XML data
XML
if you want to query XML content later
typed XML with XML SCHEMA COLLECTION
only if you have a fixed xml schema (XSD) which will never ever change. (ALTER XML SCHEMA COLLECTION does not support update or delete of XML entities as I understand)

Need WPF XML Binding Advice

I have a C# WPF program that needs to display GridView and 2D graph data which initially will come from a hardware device. I also want to continuously (or frequently periodic) backup this data to an XML file on disk. What would be the easiest way to implement this in visual studio? Should I create an XML schema first, or use the dataset designer? Should I bother with datasets at all or would it make sense to eliminate them and write my incoming data directly to xml?
I would recommend:
Plan a structure of an XML ahead. Create a simple empty file to help you along the way.
Create a data serialization provider as well as the interface that it will implement. In your case it will be an XML provider (who knows, you may need to save the data to a database in future. You should plan ahead for that.)
Write a custom class that serializes your poco domain objects into an xml using LinqToXML.

Saving file system metadata

I want to save all the metadata connected to a file system, but not the "useful" data. The metadata should be available for viewing even when the original files aren't.
I first thought that I could accomplish this by serializing for example a DirectoryInfo object, but I now understand that the object doesn't actually save the data but rather merely saves the path and accesses the file itself when the methods are called. Thus serialization would be worthless, since the deserialized object would look for the file instead of "remembering" the metadata.
So: is there some kind of built in framework class for doing this or should I just implement it myself?
This object is an object hierarchy so it could get a bit tricky to serialize? You might try creating an a simple object to model the data you want to save. You could then use AutoMapper to copy the data over into the DTO-like object and then serialize that. This way if you wanted to actually persist the entire tree of data you could without writing much code.

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
Store the Serialized obejct of the class
Store the details of that class as string.
What is the advantage of storing the serialized object in the database over storing its values as multiple strings?
-Ram
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.
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.
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.
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.

Is it a good idea to store serialized objects in a Database instead of multiple xml text files?

I am currently working on a web application that requires certain requests by users to be persisted. I have three choices:
Serialize each request object and store it as an xml text file.
Serialize the request object and store this xml text in a DB using CLOB.
Store the requests in separate tables in the DB.
In my opinion I would go for option 2 (storing the serialized objects' xml text in the DB). I would do this because it would be so much easier to read from 1 column and then deserialize the objects to do some processing on them. I am using c# and asp .net MVC to write this application. I am fairly new to software development and would appreciate any help I can get.
Short answer: If option 2 fits your needs well, use it. There's nothing wrong with storing your data in the database.
The answer for this really depends on the details. What kind of data are storing? How do you need to query it? How often will you need to query it?
Generally, I would say it's not a good idea to do both 1 and 2. The problem with option 2 is that you it will be much harder to query for specific fields. If you're going to do a LIKE query and have it search a really long string, it's going to be an expensive operation and you'll likely run into perf issues later on.
If you really want to stay away from having to write code to read multiple columns to load your data, look into using an ORM like Linq to SQL. That will help load database tables into objects for you.
I have designed a number of systems where storing 'some' object as serialized xml in the db has proven the better choice. I also learned lessons where storing objects in the db as xml ended up causing more headaches down the road. So I came up with some questions that you have to answer yes to in order to be comfortable in doing:
Does the object need to be portable?
Is the data in the object encapsulated i.e. not part of something else, and not made up of something else.
In the future can number 2 change?
In SQL you can always create a table view using XQuery, but I would only recommend you do this if a) its too late to change your mind b) you don't have that many objects to manage.
Serializing and storing objects in XML has some real benefits, especially for extensibilty and agile development.
If the number of this kind of objects is large and the size of it isn't very large. I think that using the database is a good idea.
Whether store it in a separate table or store it in the original table depends on how would you use this CLOB data with the original table.
Go with option 2 if you will always need the CLOB data when you access the original table.
Otherwise go with option 3 to improve performance.
You need to also think about security and n-tier architecture. Storing serialized data in a database means your data will be on another server, ideal if the data needs to be secure, but will alos give you network latency, whereas storing the data in the filesystem will give you quicker IO access, but very limited searching ability.
I have a situiation like this and I use the database. It also gets backed up properly with the rest of the related data.

Categories

Resources