[英]Observable (Rx) for a continuous (async) stream of objects in repository
I got a Repository
of type Item
My goal is to have the Repository
provide an Observable
or basically a "stream" of Item
using an Observable. 我有一个类型为
Item
的Repository
我的目标是让Repository
使用Observable
提供一个Observable
或基本上是一个Item
的“流”。
I'm a complete beginner regarding RX but think I got a good grasp of the basics. 我是RX的初学者,但我认为我已经掌握了基础知识。 My problem has been finding a way to keep an
Observable
alive and manually push Item
to it as they come in (which might be sockets or anything else async based). 我的问题是找到一种方法来保持
Observable
活着并在它们进入时手动将Item
推送到它(可能是套接字或其他任何基于异步的)。 Basically a Hot Observable. 基本上是一个热的观察者。
I got it working using a Subject<Item>
to which I subscribe but I'm missing the power of a "regular Observable", ie LINQ queries to filter the stream. 我使用了我订阅的
Subject<Item>
,但是我错过了“常规Observable”的功能,即LINQ查询来过滤流。 I'm afraid that I'm overlooking something or looked past a fitting approach. 我担心我会忽视某些东西或者看起来过着合适的方法。
Any insight or direction for a beginner like me would be highly appreciated. 像我这样的初学者的任何见解或方向将受到高度赞赏。
See http://rxwiki.wikidot.com/101samples#toc47 请参阅http://rxwiki.wikidot.com/101samples#toc47
Make your class native to IObservable
使您的类本机为IObservable
If you are about to build new system, you could consider using just IObservable.
如果您要构建新系统,可以考虑仅使用IObservable。
Use Subject as backend for IObservable
使用Subject作为IObservable的后端
class UseSubject { public class Order { private DateTime? _paidDate; private readonly Subject<Order> _paidSubj = new Subject<Order>(); public IObservable<Order> Paid { get { return _paidSubj.AsObservable(); } } public void MarkPaid(DateTime paidDate) { _paidDate = paidDate; _paidSubj.OnNext(this); // Raise PAID event } } private static void Main() { var order = new Order(); order.Paid.Subscribe(_ => Console.WriteLine("Paid")); // Subscribe order.MarkPaid(DateTime.Now); } }
You're probably missing a using System.Reactive.Linq
reference, otherwise Select et. 你可能错过了
using System.Reactive.Linq
引用,否则选择了et。 al. 人。 should show up on the
Subject<T>
. 应该出现在
Subject<T>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.