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:
- retrieve the current values related to a GUID;
- compare these values with some data in order to obtain new values;
- overwrite the current values with the new values.
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.
- 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. - What disadvantages could have the local database than the current “XML/Dictionary based” approach?
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/finallyand writing the XML in thefinally, 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.