【发布时间】:2011-06-06 21:39:42
【问题描述】:
我在选题时遇到了困难
public Subject<IEnumerable<Person>> PersonDataSubject;
并将其转换为:
public Subject<IEnumerable<BornInYear>> BornInYearSubject;
...使用一些 linq 聚合。
下面的示例将其置于更多上下文中,而我正在努力研究如何通过订阅 PersonDataSubject 将 IEnumerable 放入 BornInYearSubject 中。
无论我尝试什么,最终都会得到IObservable<BornInYear>,而不是IObservable<IEnumerable<BornInYear>>。
目标是让该类的客户能够订阅两个主题并在每个“下一个”通知中获得相应类型的 IEnumerable。
public class ReactiveTest
{
public class Person
{
public string name;
public DateTime dob;
};
public class BornInYear
{
public int Year;
public int Count;
}
public Subject<IEnumerable<Person>> PersonDataSubject = new Subject<IEnumerable<Person>>();
public Subject<IEnumerable<BornInYear>> BornInYearSubject= new Subject<IEnumerable<BornInYear>>();
public void LoadData()
{
// Go to hypotheritical web service and get batch of people.
IEnumerable<Person> people = WebService.Fetch();
// Notify subscribers we have a fresh batch of data.
PersonDataSubject.OnNext(people);
}
public ReactiveTest()
{
// Hookup BornInYearSubject to listen to PersonDataSubject and publish the summarised data.
PersonDataSubject.Subscribe(pd => pd.GroupBy(p => p.dob.Year)
.Select(ps => new BornInYear { Year = ps.Key, Count = ps.Count()})
.AsParallel()
);
// How do I get the results of this out and published onto BornInYearSubject?
}
}
现在我知道我可以使用 Task.Factory.StartNew(...)... 作为我对 PersonDataSubject 的 OnNext 订阅来实现这一点,但我相信它一定有可能保持更多的反应性?
【问题讨论】:
-
使用 IObservable
> 有什么问题?
标签: ienumerable system.reactive task-parallel-library