首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我应该在我的接口上公开IObservable<T>吗?

我应该在我的接口上公开IObservable<T>吗?
EN

Stack Overflow用户
提问于 2012-07-09 19:05:28
回答 4查看 1.2K关注 0票数 20

我和我的同事有争执。我们正在编写一个处理大量数据的.NET应用程序。它接收数据元素,根据某种标准将它们的子集分组为块,并对这些块进行处理。

假设我们有类型为Foo的数据项逐个到达某个源(例如,来自网络)。我们希望收集Foo类型的相关对象的子集,从每个这样的子集构造一个Bar类型的对象,并处理Bar类型的对象。

我们中的一个人建议了下面的设计。它的主题是直接从组件的接口公开IObservable<T>对象。

代码语言:javascript
复制
// ********* Interfaces **********
interface IFooSource
{
    // this is the event-stream of objects of type Foo
    IObservable<Foo> FooArrivals { get; }
}

interface IBarSource
{
    // this is the event-stream of objects of type Bar
    IObservable<Bar> BarArrivals { get; }
}

/ ********* Implementations *********
class FooSource : IFooSource
{
    // Here we put logic that receives Foo objects from the network and publishes them to the FooArrivals event stream.
}

class FooSubsetsToBarConverter : IBarSource
{
    IFooSource fooSource;

    IObservable<Bar> BarArrivals
    {
        get
        {
            // Do some fancy Rx operators on fooSource.FooArrivals, like Buffer, Window, Join and others and return IObservable<Bar>
        }
    }
}

// this class will subscribe to the bar source and do processing
class BarsProcessor
{
    BarsProcessor(IBarSource barSource);
    void Subscribe(); 
}

// ******************* Main ************************
class Program
{
    public static void Main(string[] args)
    {
        var fooSource = FooSourceFactory.Create();
        var barsProcessor = BarsProcessorFactory.Create(fooSource) // this will create FooSubsetToBarConverter and BarsProcessor

        barsProcessor.Subscribe();
        fooSource.Run(); // this enters a loop of listening for Foo objects from the network and notifying about their arrival.
    }
}

另一个人提出了另一种设计,它的主要主题是使用我们自己的发布者/订阅者接口,并仅在需要时在实现中使用Rx。

代码语言:javascript
复制
//********** interfaces *********

interface IPublisher<T>
{
    void Subscribe(ISubscriber<T> subscriber);
}

interface ISubscriber<T>
{
    Action<T> Callback { get; }
}


//********** implementations *********

class FooSource : IPublisher<Foo>
{
    public void Subscribe(ISubscriber<Foo> subscriber) { /* ...  */ }

    // here we put logic that receives Foo objects from some source (the network?) publishes them to the registered subscribers
}

class FooSubsetsToBarConverter  : ISubscriber<Foo>, IPublisher<Bar>
{
    void Callback(Foo foo)
    {
        // here we put logic that aggregates Foo objects and publishes Bars when we have received a subset of Foos that match our criteria
        // maybe we use Rx here internally.
    }

    public void Subscribe(ISubscriber<Bar> subscriber) { /* ...  */ }
}

class BarsProcessor : ISubscriber<Bar>
{
    void Callback(Bar bar)
    {
        // here we put code that processes Bar objects
    }
}

//********** program *********
class Program
{
    public static void Main(string[] args)
    {
        var fooSource = fooSourceFactory.Create();
        var barsProcessor = barsProcessorFactory.Create(fooSource) // this will create BarsProcessor and perform all the necessary subscriptions

        fooSource.Run();  // this enters a loop of listening for Foo objects from the network and notifying about their arrival.
    }
}

你认为哪一个更好?公开IObservable<T>并使我们的组件从Rx操作符创建新的事件流,或者定义我们自己的发布者/订阅者接口并在需要时在内部使用Rx?

以下是关于设计的一些需要考虑的事项:

  • 在第一个设计中,我们界面的消费者拥有Rx的全部功能,并且可以执行任何Rx操作符。其中一人声称这是一种优势,另一人则声称这是drawback.
  • The的第二种设计,允许我们在幕后使用任何发布者/订阅者架构。第一个设计将我们与Rx捆绑在一起。
  • 如果我们希望使用Rx的功能,它需要在第二个设计中做更多的工作,因为我们需要将自定义的发布者/订阅者实现转换为Rx并来回转换。它需要为每个希望进行一些事件处理的类编写胶水代码。
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11393818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档