RX 主题 - 应该避免它们吗? [英] RX Subjects - are they to be avoided?

查看:22
本文介绍了RX 主题 - 应该避免它们吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个主题中进行了关于该主题的小型讨论,并且希望人们对主体的坏"面.

I've had a mini-discussion on the topic in another thread, and would like to have people's input on the "bad" sides of subjects.

经常访问 RX 论坛的人都知道 E.Meijer 不喜欢科目.虽然我非常尊重 RX 创建者的意见,但几年来我一直在多个项目中广泛使用 Subjects,并且没有因为它们而出现任何架构问题或错误.

People who frequent the RX forum know that E.Meijer does not like Subjects. While I have a deepest respect to RX creator's opinion, I have been using Subjects quite extensively in multiple projects for a couple of years and haven't had any architectural problem or a bug because of them.

我能说出的主题的唯一陷阱"是它们不是可重用的"——在你完成了一个主题的 observable 之后,你需要重新实例化它,然后新的订阅者才能从它接收事件.

The only "pitfall" with Subjects I can name is that they are not "reusable" - after you have completed an observable on a Subject, you need to re-instantiate it, before new subscribers can receive events from it.

代码味道"和不喜欢它们"需要务实"的例子来支持——当使用主题可能导致错误或问题时,你能提醒我们注意可能的情况吗?或者,也许您认为它们完全简单且无害 - 然后尝试定义它们的使用区域.

"Code smell" and "Don't like them" need to be supported by "pragmatic" examples - can you bring to our attention possible situations when using a Subject can lead to a bug or a problem? Or maybe you think they are easy and harmless altogether - then try to define an area where they are to be used.

推荐答案

Erik Meijer 正在以一种纯函数式的方式思考——主题是 Rx 的可变变量.因此,在一般用法中,他是对的 - 使用 Subjects 有时是一种摆脱功能性思考的方法,如果你使用它们太多,你就会试图逆流而上.

Erik Meijer is thinking in a purely functional way - Subjects are the mutable variables of Rx. So, in general usage he's right - using Subjects is sometimes a way to cop out of Thinking Functionally, and if you use them too much, you're trying to row upstream.

但是!当您与 .NET 的非功能性世界交互时,Subject 非常有用.包装事件或回调方法?主题是伟大的.试图将 Rx接口"放到一些现有代码上?使用主题!

However! Subject are extremely useful when you're interfacing with the non-Functional world of .NET. Wrapping an event or callback method? Subjects are great for that. Trying to put an Rx "interface" onto some existing code? Use a Subject!

这篇关于RX 主题 - 应该避免它们吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆