开发者

How do you persist data to disk from .NET?

I have a variety of rich data structures (primarily trees) that I would like to persist to disk, meaning I not only want to write them to disk but I want a guarantee that the data has been fully written and will survive a power-down.

Others seem to design ways to encode rich data structures in flat database tables as lookup tables from parent to child nodes. This facilitates running SQL queries against the data but I have no need for that: I just want to save and load my trees.

The obvious solution is to store everything as a blob in the data base: a single entry perhaps containing a long string. Is that an abuse of the database or a recommended practice? Another solution might be to use an XML database? Are there any alternatives to databases that I should be considering?

Finally, I'm doing this from F# so a turnkey solution for persisting data from .NET would be ideal...

EDIT: Please note that formatting (e.g. serialization) is irrelevant as I can trivially convert between formats with F#. This is about getting an acknowledgement that a write has been completed all the way down to the non-volatile store (i.e. the disk platter) and no part of the written data is still being held in volatile store (e.g. an RAM cache) so that I can continue safe in that knowledge (e.g. by deleting the old version of th开发者_开发知识库e data from disk).


Some of the constructors for .NET's FileStream class take a parameter of type FileOptions. One of the values for FileOptions is WriteThrough, which "Indicates that the system should write through any intermediate cache and go directly to disk."

This should ensure that by the time your write operation (to a new file) returns, the data is committed to disk and you can safely delete the old file.


This can be done via Serialization.

The .NET Framework includes many built-in options for serializing your data to disk, including using binary or XML-based formats. Detailed How-To articles are provided in the MSDN Documentation.


In order to do this, you will require a resource which will allow you to engage in a Transaction (more often than not, you would use a TransactionScope.

Most databases will participate in a Transaction if one is contained. Disk operations can also be managed by a Transaction, but you would have to do some specific work in order to utilize it in .NET.

Also, note that this is only available on Windows Vista and later.

If you go the database route, then you could store the serialized contents of your trees in a blob (or text, depending on the serialization mechanism).

Note, you can also use the FILESTREAM functionality in SQL Server (2008 and up, I believe) to store your files on the filesystem and gain the benefits of transactions in SQL Server.


I haven't used db4o from F# before, but it's all about persisting CLR object graphs to disk in a transactional manner. If it works with records and discriminated unions, it might suit you.

Edit: I just tested db4o 8.0 (.NET 4 version) and it seems to handle both record types and discriminated union hierarchies perfectly well.


Try using XMLSerializer (System.Xml.Serialization).

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

It can automatically persist complex data structures based on their properties, and you can use attributes to control the output, if you wish:

http://msdn.microsoft.com/en-us/library/83y7df3e.aspx


Slightly OT as the OP didn't want XML, but seeing others mentioned the XML formatter... If you want textual persistence, the SoapFormatter handles cases (cycles/object-graphs) that the default XML formatter does not - its XML is not as readable as XMLFormatter's, but it's more readable than binary :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜