-1

I have a couple questions on how to do file handling properly. I use binary serialization and deserialization in my program. On top of that I also export and import text files. I listed a example below of my serialization code. I use an openfile dialog to select folders/files.

This is my binary serialization method:

if (string.IsNullOrWhiteSpace(fileName))
{
    throw new ArgumentOutOfRangeException("fileName");
}

FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate);

try
{
    BinaryFormatter binaryFormatter = new BinaryFormatter();
    binaryFormatter.Serialize(fileStream, Animals);
}
finally
{
    fileStream.Close();
}

And these are the exceptions i catch:

try
{
    administration.Load(fileName);
}
catch (NotSupportedException nonSupportedException)
{
    MessageBox.Show(nonSupportedException.Message);
}
catch (UnauthorizedAccessException unauthorizedAccesException)
{
    MessageBox.Show(unauthorizedAccesException.Message);
{
catch (SecurityException securityException)
{
    MessageBox.Show(securityException.Message);
}
catch (DirectoryNotFoundException directoryNotFoundException)
{
    MessageBox.Show(directoryNotFoundException.Message);
}
catch (IOException IOException)
{
    MessageBox.Show(IOException.Message);
}

I catch the same exceptions for deserialization. The only difference there is these 3 lines:

if (File.Exists(fileName)) { }
FileStream fileStream = new FileStream(fileName, FileMode.Open);
Animals = formatter.Deserialize(fileStream) as List<Animal>;

What should I do if I get wrong data? For example: half of the file has the right data and the other half doesn't.

How should i write unit tests for serialization? Many exceptions like SecurityException are hard to test.

What exceptions should I catch? I looked at MSDN, but I am not sure if I should just straight up catch all of the exceptions listed. I deliberately don't catch the ArgumentOutOfRange exception for example, because I don't want to catch programming mistakes.

I have the same questions for reading and writing text files. Is there a difference in testing/exception catching? The only difference is that I use StreamWriter for writing text files. And I use File.ReadAllLines to read the text file I select.

Thank you.

Mathijs F
  • 49
  • 1
  • 8
  • To be 100% honest, all of your questions seem to be requirements that you or your end user/customer need to answer. As far as writing tests for serialization: http://stackoverflow.com/questions/236599/how-to-unit-test-if-my-object-is-really-serializable – Stephen P. Apr 05 '17 at 18:15

1 Answers1

1

Your question is not really a good fit for Stack Overflow as your requirements largely depend on things you haven't told us, and probably don't know yourself yet. You have to ask your customer what they want, figure out the simplest thing that could possibly work, send that to your customer and have them try it out, then incorporate fixes into the next iteration. Do not think you can design out everything in advance and that your first attempt will be perfect.

Using binary serialization for data files is a mistake. The format is poorly documented and subject to change. You can't use it from other platforms, or even from incompatible frameworks in the same platform. It also has very poor performance for hierarchical data structures. If your needs are simple, start with text JSON or XML files. If you have complex data or need to enforce data consistency you should use SQL.

Code like this is usually a mistake:

catch (NotSupportedException nonSupportedException)
{
    MessageBox.Show(nonSupportedException.Message);
}

catch is for fixing errors and MessageBox.Show does not fix any errors; it hides errors then continues on as if nothing went wrong. But something did go wrong; you need to figure out what and fix it. If you can't fix it then do not catch the error; let enclosing methods handle it.

Community
  • 1
  • 1
Dour High Arch
  • 21,513
  • 29
  • 75
  • 90