Is a Subject in RX.Net always harmful?
c#, reactive-programming, system.reactive
Solution
Subjects are not always harmful. There are many legitimate uses of them even within Rx itself. However, many times a person goes to use a Subject, there's already a robust Rx method written for that scenario(and it may or may not be using subjects internally). This is the case for your 2 examples. Look at Task.ToObservable and Observable.FromEventPattern.
Another common case subjects are misused is when a developer breaks a stream in two. They become convinced they need to subscribe to a stream and in the callback they produce data for a new stream. They do this with a Subject. But usually they just should have used Select instead.
Problem
I was talking to a colleague who pointed me to the SO question about subjects being considered harmful. However, I have two cases where I have some non-deterministic code that does not seem reasonable any other way. Non-standard event: ``` event handler(class, result) { subject.OnNext(result); } public delegate void _handler ([MarshalAs(UnmanagedType.Interface), In] MyClass class, [MarshalAs(UnmanagedType.Interface), In] ResultClass result) ``` Parallel Tasks (Non-Deterministic number of tasks all running in parallel, starting at different times): ``` Task.Start(()=> ...).ContinueWith(prevTask => subject.OnNext(prevTask.result)) ``` The subject is not exposed, only through an observable. Is there another route suggested that isnt a ton of boilerplate?