How to avoid the use of Subjects in RX

后端 未结 2 1131
南笙
南笙 2021-02-19 13:55

So I keep reading everywhere that use of Subject is \"bad\" - and I kind of agree with the reasoning.

However, I am trying to think of the best way

2条回答
  •  北海茫月
    2021-02-19 14:29

    It's not so much that the use of Subject is bad - there has to be some way of "entering the monad" - that's the academic way of saying "get an IObservable". You need to start somewhere.

    The problem with Subject arises more when it's used from a subscription instead of chaining existing observables together. Subjects should just exist at the edges of your Rx machinery.

    If none of the provided entry points (e.g. FromEvent, FromEventPattern, FromAsync, Return, ToObservable() and so on) work for you then using Subject is perfectly valid. And there's no need to add extra complexity just to facilitate using one of the above - most of them use subjects or subject-like constructs under the covers anyway.

    In your case, sounds like Subject is just fine. You might what to look at exposing it via AsObservable() in order to hide the implementation details.

提交回复
热议问题