I am using the Reactive Extensions (Rx) Subject as a direct replacement for C# events like so:
public class MyClass
{
private Subject<string> subject;
public IObservable<string> WhenSomethingHappened
{
get { return this.subject.AsObservable(); }
}
private void OnSomethingHappened(string something)
{
this.subject.OnNext(something);
}
}
Note that I never call OnCompleted on my subject. Should MyClass be implementing IDisposable and calling this.subject.Dispose? This would mean that any implementation using Subject should implement IDisposable.
The reason I ask is that the IDisposable pattern is a bit like a disease, if one thing implements it, everything that uses it has to implement it too.
Nope, you don't really need to do this. When worrying about memory usage and lifetimes, think about disposing Subscriptions, not Subjects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With