The question is entitled with the word "Stream" because the question below is a concrete example of a more generic doubt I have about Streams:
I have a problem that accepts two solutions and I want to know the best one:
If the write to DB fails I'll have to download again in the second case, but not in the first case.
Which is best? Which would you use?
Unless the increased latency is really killing you, I'd usually go for Option 1 unless there's a good reason you don't want the data on the file system (e.g. concerns about security, capacity, ...).
Or maybe Option 3 as suggested by Max Schmeling, save to the filesystem at the same time as writing to the database.
Disk space is cheap, and it's often useful to have a backup of downloaded data (e.g. to test changes to your database writing code, as evidence of the contents of data downloaded, ...).
To detail Jekke's reply:
Depending on the file system creates many occasions for failure (you must create a valid file name, make sure the file system isn't full, make sure the file can be opened and written to by you but not by anyone else, what about concurrent use, etcetera).
The only benefit of writing to file I can think of is that you'll know the download completed successfully prior to doing anything with the database. If you can hold the contents in memory, do that instead. If you can't and really insist on not going to the database in case of an interrrupted download, at least use .NET's built-in support to help you with the tricky bits (e.g. IsolatedStorageFileStream).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With