Reactive Extension 中的数据服务 [英] Data service in Reactive Extension

查看:27
本文介绍了Reactive Extension 中的数据服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个通用类,用于数据处理创建、更新和删除的内存缓存.底层模型继承自具有字符串类型 Id 的接口.

interface IModel{字符串 ID { 获取;}}

处理创作和更新很容易.例如,如果我想订阅流并填充字典,我知道如果模型 Id 不存在,则需要创建,否则它是更新.

我的问题是:
您将如何处理删除而不引入另一个类来包装我的模型?我想保留一个 IObservable,而不是 IObservable>IObservable>,但我不知道如何.这可能吗?

interface IDataService{IObservable数据流{获取;}}

解决方案

正如@Enigmativity 建议的那样,您可以使用嵌套的可观察序列来解决这个问题.这在 IntroToRx 的巧合序列部分有所涉及.>

这将如何运作?

您可以将嵌套序列视为二维数组,或者更具体地说是 锯齿状数组.外部序列是内部序列的容器.内部序列的到来代表了一个模型的创建.

interface IDataService{IObservable数据流{获取;}}

一旦你有了一个内部序列,它产生的所有值都是更新(除了第一个).内部序列只会为单个 id 生成更新.当内部序列完成时,代表一个删除.

这种模式适用于各种用例,如上面链接的第一段所述.

作为一个大理石图,您将有如下内容.每行代表一个内部序列.

m1 1---2----3---|m2 a----|m3 x----y---z--

这将导致以下逻辑流程:

  1. 创建状态为1"的 m1
  2. 创建状态为 'a' 的 m2
  3. 用值2"更新 m1
  4. 创建值为 'x' 的 m3
  5. 删除 m2
  6. 用值3"更新 m1
  7. 用值 'y' 更新 m3
  8. 删除 m1
  9. 用值 'z' 更新 m3

I want to have a generic class for an in-memory cache of data handling creations, updates and deletions. Underlying model inherits from an interface with an Id of type string.

interface IModel
{
  string Id { get; }
}

Handling creations & updates is easy. For instance, if I want to subscribe to the stream and populate a Dictionary, I know that a creation is needed if the model Id is not already there, otherwise it is an update.

My question is:
How would you handle deletions without introducing another class to wrap my models? I would like to keep an IObservable<TModel>, not something like IObservable<Event<TModel>> or IObservable<Pair<string, TModel>>, but I don't see how. Is that possible?

interface IDataService<TModel>
{
  IObservable<TModel> DataStream { get; }
}

解决方案

As @Enigmativity suggested you could use nested observable sequences to solve this problem. This is touched on in the Sequences of coincidence section in IntroToRx.

How would this work?

You can think of nested sequences to be something like a 2d Array, or even more specifically a jagged array. The outer sequence is the container for the inner sequences. The arrival of an inner sequence represents a creation of a model.

interface IDataService<TModel>
{
    IObservable<IObservable<TModel>> DataStream { get; }
}

Once you have an inner sequence all values that it produces are updates (except for the first one). An inner sequence will only produce updates for a single id. When the inner sequence completes, the represent a deletion.

This pattern works well for various use-cases, as outlined in the initial paragraph in the link above.

As a marble diagram you would have something like the following. Each row represents an inner sequence.

m1  1---2----3--|
m2     a----|
m3       x----y---z--

This would result in the following logical flow:

  1. Create m1 with the state of '1'
  2. Create m2 with the state of 'a'
  3. Update m1 with value '2'
  4. Create m3 with value 'x'
  5. Delete m2
  6. Update m1 with value '3'
  7. Update m3 with value 'y'
  8. Delete m1
  9. Update m3 with value 'z'

这篇关于Reactive Extension 中的数据服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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