Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET streams, passing streams between objects, best practices (C#)

Tags:

c#

stream

I'm currently writing a little toy assembler in c# (going through the elements of computing systems book. Really good book by the way.)

The assembler takes an input file path and removes junk (comments etc) lines.

The file is then passed to a parser then finally to a another module that creates the binary code.

This isn't too complicated, but I'd like not to have to write out a temporary file to the filesystem every time one object has finished it's processing of the input file.

I'd like to just pass the stream onto the next object. I originally thought each class involved in the parsing / junk removing would implement IDisposable but I think this means I can't pass the stream on the next object for processing (the stream would be closed, unless I keep it all in one using statement?).

I think I'm missing something here, is there a simple way to pass streams between objects cleanly, or do I need a different approach?

Thanks in advance for any help!

like image 482
bplus Avatar asked Jun 24 '09 22:06

bplus


3 Answers

The way I did these projects in TECS is:

  • read each line in the file
  • trim the whitespace at the beginning and end of the line
  • if the line is blank or if it starts with // then go to the next line
  • otherwise, store the line in an array (in C#, I actually use a List<string> object)

Once I've gone through all of the lines, I can close my file stream and safely do my work on the array of lines.

like image 166
Dinah Avatar answered Nov 07 '22 01:11

Dinah


In general, it is the responsibility of the consumer to properly dispose of a Disposable object. As such, if you pass off a Stream to another object, you shouldn't Dispose it - that would be the responsibility of the consumer.

So in the clear-cut scenarios, either you hold a reference to a Disposable object, in which case you should ensure that it is properly disposed; or you pass the reference to someone else and forget about it.

Then what about the cases where you need to hold a reference yourself, but still pass it along? In these cases, pass a copy of the Disposable resource - this will alow you and the consumer to manage the lifetime of the two instances independently of each other. However, if you get into this situation, you should reconsider your design, as I would call that a code smell.

like image 21
Mark Seemann Avatar answered Nov 06 '22 23:11

Mark Seemann


If something else is using the stream after the assembler is done with it, the assembler shouldn't "own" the stream. The caller should either create a stream for the assembler (and subsequent modules) to use, or the assembler should return a new stream which it is then the caller's responsibility to close.

It would be instructive to see some more details on what your program's architecture looks like and what methods we are discussing here.

like image 2
David Avatar answered Nov 06 '22 23:11

David