tags:

views:

518

answers:

4

Hello, 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!

+2  A: 

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.

David
+1  A: 

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.

The 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.

Mark Seemann
Thanks for that (and thanks to the other responders).Yeah what you suggest is a lot cleaner than what I was doing.
bplus
A: 

Overall, I agree with the previous comments. However if your model doesn't fit that, you could do what Microsoft did with the XML Writer: It accepts an XMLWriterSettings parameter when you instance it, and one of the properties of the settings object describes whether the writer should close the underlying stream when the writer is disposed.

JMarsch
A: 

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.

Dinah