aateeque
aateeque

Reputation: 2231

Should an IObservable<IEnumerable<T>> really be an IObservable<T>?

Consider a method DoSomething() which returns:

  1. IObservable<IEnumerable<T>> DoSomething()
  2. IObservable<T> DoSomething()

Considering both:

Upvotes: 0

Views: 304

Answers (3)

Enigmativity
Enigmativity

Reputation: 117027

IObservable<IEnumerable<T>> does differ semantically from IObservable<T>.

IObservable<IEnumerable<T>> represents zero or more moments in time where zero or more values are reported.

IObservable<T> represents zero or more moments in time where a single value is reported.

The former can then be used to represent the activity between points in time and returning an empty enumerable would positively represent nil activity since the last value. The latter cannot explicitly do that.

These two forms can therefore be used to represent two different real-world ways or reporting values.

Upvotes: 1

Niall Connaughton
Niall Connaughton

Reputation: 16107

There's nothing wrong with returning IObservable<IEnumerable<T>> if that's what your method means. Buffer is one example where it makes sense - anything with batching would make sense. If you had a service that accepted batched requests for orders, then an IObservable<IEnumerable<Order>> could be valid. You could flatten it, and that might also be valid.

It depends whether the concept of the batch/buffer itself is integral to what your method is supposed to do. If your method just happens to be using buffer to achieve its aims, but the method isn't a batch operation by nature, then it will probably be more intuitive to flatten the Observable.

Upvotes: 0

Timothy Shields
Timothy Shields

Reputation: 79441

Consider the signature of this Observable.Buffer extension method:

IObservable<IList<TSource>> Buffer<TSource>(IObservable<TSource> source, int count)

Clearly it wouldn't be very useful if the result was flattened to an IObservable<TSource>.

As you said yourself, IObservable<T> and IObservable<IEnumerable<T>> are conceptually different.

Upvotes: 2

Related Questions