What is the best way to pass a stream around
You may not realize it, but you are attempting to implement the pipeline design pattern. As a starting point, consider taking a look at:
- MSDN: Pipelines
- Steve's Blog: Pipeline Design Pattern
- (other good references??)
With regards to your implementation, I recommend that you go with option #2:
public interface IStreamHandler
{
void Process(Stream stream);
}
With regards to object lifetime, it is my belief that:
- the implementation should be consistent in how it handles calling
Dispose
- your solution will be more flexible if
IStreamHandler
did not callDispose
(now you can chain handlers together much like you would in Unix pipes)
THIRD-PARTY SOLUTIONS
Building a pipeline solution can be fun, but it is also worth noting that there are existing products on the market:
- Yahoo: Pipes
- Microsoft: BizTalk
- IBM: Cast Iron
- StackOverflow: Alternatives to Yahoo Pipes
ADDITIONAL NOTES
There is a design issue related to your proposed Option 2:
void Process(Stream stream);
In Unix Pipes you can chain a number of applications together by taking the output of one program and make it the input of another. If you were to build a similar solution using Option 2, you will run into problems if you are using multiple handlers and your data Stream
is forward only (i.e. stream.CanSeek=False
).
Option 2_2 is the standard way of dealing with disposable resources.
Your SomeTestClass
instance asks the producer for a stream - then SomeTestClass
owns a stream and is responsible for cleaning up.
Options 3 and 2_1 rely on a different object to clean up the resource owned by SomeTestClass
- this expectation might not be met.
Option 1 is jut copying a stream's content to another stream - I don't see any benefits in doing that.