何时使用Rx主题 [英] When to use Rx Subjects

查看:60
本文介绍了何时使用Rx主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 


我有几个用例,我认为我需要创建一个主题< T>对于。但是我知道,通常我们不应该明确地创建主题,而是赞成  工厂方法。在这些情况下,虽然我不确定
工厂方法是否合适。



场景1: - 工作流程状态


我有一个可以处于多种状态的特定工作流,比如说InitialState,LoggingIn,LoggedIn,Disconnected,Connected ,.我需要将当前状态公开给我班级的消费者。我可以使用一个简单的CurrentState属性和
一个Action< state>通知更改。但是我认为它是Rx的一个很好的候选者,因为消费者可以订阅,获取当前状态,然后通知任何变化。这似乎指向ReplaySubject(总是给出当前的
状态)。当前状态的条件是由我无法控制的被覆盖方法的数量决定的。



例如。 OnLogonStateReceived(状态){},  OnConnectedStateReceived(状态){}


我可以在每个方法上创建一个自定义事件,然后使用FromEvent模式,但这似乎是无其他原因介绍活动。


有没有其他方法可以做到这一点?



场景2: -  


这与第一个场景很相似,因为我有一个覆盖方法钩子来获取一些消息。这次只是一种方法。例如


OnSomeDataResponse(SomeData数据){}


我想创建一个Observable接收这些数据类型,以便我的消费者可以处理它们。 / p>


感谢任何帮助/指导。



解决方案


 


我有几个用例我认为我需要创建一个主题< T>对于。但是我知道,通常我们不应该明确地创建主题,而是赞成  工厂方法。在这些情况下,虽然我不确定
工厂方法是否适合。





当然你可以明确地创建主题,你混淆建议没有显式实现IObservable,而是使用Observable.Create,建议不要在查询管道中使用Subjects,这是有道理的 - 你想要
Subject是一个IObservable,你可以轻松将主题转换为。 受试者在"终点"上是正确的。你的申请。 将它们设为私有/受保护并仅通过属性作为IObservable公开Subject,这样您就可以确保
某人没有开始在您的管道中的某个地方向您的主题提供内容!  这是推荐, AFAIK。


Hi, 

I have a couple of use cases that I think I'll need to create a Subject<T> for. However I'm aware that generally we shouldn't be creating subjects explicitly, instead favoring the factory methods. In these cases though I'm not sure the factory methods fit.

Scenario 1:- Workflow Status

I have a particular workflow that can be in a number of states, lets say InitialState, LoggingIn, LoggedIn, Disconnected, Connected, . I need to expose the current state to the consumer of my class. I could do this using a simple CurrentState property and an Action<state> to notify changes. However I think it makes a good candidate for Rx in that the consumer can subscribe, get the current state and then be notified of any changes. This would seem to point to a ReplaySubject (to always give the current state). The condition of the current state is decided from number of overridden methods which I have no control over.

e.g. OnLogonStateReceived(State state){}, OnConnectedStateReceived(State state){}

I could create an custom event on each of these methods and then use the FromEvent pattern but this would seem to introduce an event for no other reason.

Are there any alternative ways to do this?

Scenario 2:- 

This is quite similar to the first scenario in that I have an overrriden method hook to get some messages. This time it is only one method though. e.g.

OnSomeDataResponse(SomeData data){}

I would like to create an Observable of these data types received so my consumer can process them.

Any help/guidance appreciated.

解决方案

Hi, 

I have a couple of use cases that I think I'll need to create a Subject<T> for. However I'm aware that generally we shouldn't be creating subjects explicitly, instead favoring the factory methods. In these cases though I'm not sure the factory methods fit.


Sure you can create Subjects explicitly, you are mixing up the recommendation to not implement IObservable explicitly, instead use Observable.Create, with the recommendation to not use Subjects within a query pipeline, which makes sense - there you want the Subject to be an IObservable, which you can easily convert a Subject to.  Subjects are fine on "end points" of your application.  Make them private/protected and expose the Subject only through a property as an IObservable, that way you can ensure that someone doesn't start feeding stuff into your Subject somewhere in your pipeline!  That's the recommendation, AFAIK.


这篇关于何时使用Rx主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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