I want to create a class that can be used to represent a dynamically computed value, and another class that represents a value can be the source (subject) for these dynamically computed values. The goal is that when the subject changes, the computed value is updated automatically.
It seems to me that using IObservable/IObserver is the way to go. Unfortunately I can’t use the Reactive Extensions library, so I am forced to implement the subject/observer pattern from scratch.
Enough blabla, here are my classes:
public class Notifier<T> : IObservable<T>
{
public Notifier();
public IDisposable Subscribe(IObserver<T> observer);
public void Subscribe(Action<T> action);
public void Notify(T subject);
public void EndTransmission();
}
public class Observer<T> : IObserver<T>, IDisposable
{
public Observer(Action<T> action);
public void Subscribe(Notifier<T> tracker);
public void Unsubscribe();
public void OnCompleted();
public void OnError(Exception error);
public void OnNext(T value);
public void Dispose();
}
public class ObservableValue<T> : Notifier<T>
{
public T Get();
public void Set(T x);
}
public class ComputedValue<T>
{
public T Get();
public void Set(T x);
}
My implementation is lifted mostly from: http://msdn.microsoft.com/en-us/library/dd990377.aspx.
So what would the “right” way to do this be? Note: I don’t care about LINQ or multi-threading or even performance. I just want it to be simple and easy to understand.
If I were you I would try to implement your classes as closely as possible to the way Rx has been implemented.
One of the key underlying principles is the use of relatively few concrete classes that are combined using a large number of operations. So you should create a few basic building blocks and use composition to bring them all together.
There are two classes I would take an initial look at under Reflector.NET:
AnonymousObservable<T>&AnonymousObserver<T>. In particularAnonymousObservable<T>is used through-out Rx as the basis for instantiating observables. In fact, if you look at the objects that derive fromIObservable<T>there are a few specialized implementations, but onlyAnonymousObservable<T>is for general purpose use.The static method
Observable.Create<T>()is essentially a wrapper toAnonymousObservable<T>.The other Rx class that is clearly a fit for your requirements is
BehaviorSubject<T>. Subjects are both observables and observers andBehaviorSubjectfits your situation because it remembers the last value that is received.Given these basic classes then you almost have all of the bits you need to create your specific objects. Your objects shouldn’t inherit from the above code, but instead use composition to bring together the behaviour that you need.
Now, I would suggest some changes to your class designs to make them more compatible with Rx and thus more composible and robust.
I would drop your
Notifier<T>class in favour of usingBehaviourSubject<T>.I would drop your
Observer<T>class in favour of usingAnonymousObserver<T>.Then I would modify
ObservableValue<T>to look like this:The implementation of
ObservableValue<T>would wrapBehaviourSubject<T>rather than inherit from it as exposing theIObserver<T>members would allow access toOnCompleted&OnErrorwhich wouldn’t make too much sense since this class represents a value and not a computation. Subscriptions would useAnonymousObservable<T>andDisposewould clean up the wrappedBehaviourSubject<T>.Then I would modify
ComputedValue<T>to look like this:The
ComputedValue<T>class would wrapAnonymousObservable<T>for all subscribers and and usesourceto grab a local copy of the values for theValueproperty. TheDisposemethod would be used to unsubscribe from thesourceobservable.These last two classes are the only real specific implementation your design appears to need – and that’s only because of the
Valueproperty.Next you need a static
ObservableValuesclass for your extension methods:The
Computemethod would use anAnonymousObservable<V>to perform the computation and produce anIObservable<V>to pass to the constructor ofComputedValue<V>that is returned by the method.With all this in place you can now write this code:
Please let me know if this is helpful and/or if there is anything that I can elaborate on.
EDIT: Also need some disposables.
You’ll also need to implement
AnonymousDisposable&CompositeDisposableto manage your subscriptions particularly in theComputeextension method. Take a look at the Rx implementations using Reflector.NET or use my versions below.