So if I'm writing an extension method for a type in another library that converts that type into an Rx IObservable<T>
, what exactly is the convention? I ask because I thought AsObservable
was the way to go, but I've also seen ToObservable
. It's unclear to me which is used when or if there's any real convention at all.
Could it be ToObservable
is reserved for turning something that is expected to product a single event into an IObservable<T>
where as AsObservable
is reserved for converting something that is expected to produce a sequence of events into an IObervable<T>
?
Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters.
The class that defines such extension methods is referred to as the "sponsor" class, and it must be declared as static.
Unless you have a very good reason to write your own cross-duality operators, you will not need to write a "To" postfix when dealing with Enumerables and Observables.
Observe the following truths:
ToObservable
is expected to convert pull-based sequences into push-based sequences. ToEnumerable
is expected to convert push-based sequences into pull-based sequences.AsObservable
is expected to wrap a push-based type as IObservable< T >
. AsEnumerable
is expected to wrap a pull-based type as IEnumerable< T >
.Therefore, To
should be used when you're writing a method which switches the duality of the source, and As
should be used when the resulting duality is the same as the source's.
In most cases, you will be using As
for your own methods, because the cross-duality operators of ToObservable
and ToEnumerable
have already been written for you.
Sources: Personal experience, MSDN documentation (above), Erik Meijer himself.
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