enzom83
enzom83

Reputation: 8310

Should I use a local database or an XML file?

I should choose a persistent storage for my application and therefore I should decide whether to opt for a local database (in SQL Server Compact 3.5 Database) or an XML file. Basically, the application needs to update very frequently some of the data in a table: each row of this table consists of a GUID (as primary key), two DateTime columns, two TimeSpan columns, and four double columns.

This table might consist of thousands of rows, and some of these rows (about 10%) are updated very frequently, where each update consists of the following operations:

The rows that are updated (the above 10%) are not always the same, but they may change gradually during program execution.

If I store this table in an XML file, as soon as the application starts, the Data Access Layer loads the data from the XML file to a Dictionary<Guid, ...> in order to obtain better performance. Then when the application terminates, the XML file is overwritten with the data updated in the dictionary, because I believe that there is no way to update specific portions of an XML file. This is the approach I currently use.

  1. What would be the advantages of using a local database instead of the XML file? I believe that if I use a local database, I could do without the Dictionary<Guid, ...>, then the Data Access Layer would be simpler.
  2. What disadvantages could have the local database than the current "XML/Dictionary based" approach?

Upvotes: 4

Views: 2222

Answers (1)

kelloti
kelloti

Reputation: 8951

If your application crashes, your XML file might not be saved. Or worse, it might be mangled. It is possible to mitigate this bug by wrapping your program in a try/finally and writing the XML in the finally, but this still opens you up to mangling the XML.

When you use a database, you guarantee that your transaction will be atomic - meaning a write will either fully complete or not happen at all. This is the primary benefit of using a database.

However, using the XML/Dictionary approach is still very fast due to the fact that it's all in-memory transactions. As long as you don't care about data integrity this is a very viable approach.

As far as a complicated data access layer, this can be an issue with either approach. Just keep this layer separate from your business logic and it shouldn't affect which you choose.

Upvotes: 5

Related Questions