Reactive Extensions(Rx) 学习

简介:

Bruce Eckel(著有多部编程书籍)和Jonas Boner(Akka的缔造者和Typesafe的CTO)发表了“反应性宣言”,在其中尝试着定义什么是反应性应用。

这样的应用应该能够:

对事件做出反应:事件驱动的本质,让反应性应用能够支持文中提到的若干特性。
对负载做出反应:聚焦于可扩展性,而不是单用户性能。
对失败做出反应:建立弹性系统,能够从各个层级进行恢复。
对用户做出反应:综合上述特征,实现交互式用户体验。
在这份宣言公布之后,Scala的创造者Martin Odersky、Reactive Extensions的创造者Erik Meijer和Akka科技公司的领导者Roland Kuhn,在Coursera上发布了一套免费课程,名为“反应性编程原理”:

该课程的目标在于讲授反应性编程的原理。反应性编程是一门新兴的学科,结合了并发、事件驱动和异步系统。对于编写任何类型的Web服务或分布式系统来说,它都至关重要;同时它在众多高性能并发系统中占有核心位置。反应性变成可以被视作高阶函数式编程对并发系统的自然拓展,通过协调和编排Actor交换的异步数据流,来处理分布的状态。

Reactive Extensions(Rx)的优点在于能够将传统的异步编程方式从支离破碎的代码调用中解放出来。Rx能够使的我们可以将异步代码写到一个单独的方法中,使得代码可读性和可维护性大大增强。

《Reactive Extensions介绍》我们了解了Rx中的一些比较重要的操作符,本文中我们将会学习如何将Reactive Extensions(Rx)应用到我们的应用程序中。

同步方法调用是阻塞式的,在很多场景下这是不合适的。我们能够用Rx改造成异步调用。一个最简单的方法就是使用IObservable.Start方法,使得Rx为我们来管理这些异步调用。

public static void ObservableStart(int x, int y)
        {
            PlusTwoNumberAsync(x, y).Subscribe(Console.WriteLine);
            Console.ReadKey();
        }

        private static IObservable<int> PlusTwoNumberAsync(int x, int y)
        {
            return Observable.Start(() => PlusTwoNumber(x, y));
        }

        private static int PlusTwoNumber(int x, int y)
        {
            Thread.Sleep(5000);
            return x + y;
        }

除了Observable.Start外也可以使用Observable.Return来将同步方法改造为异步方法。只需要将上面的PlusTwoNumberAsync方法改为下面即可,运行程序的效果相同。

private static IObservable<int> PlusTwoNumberReturnAsync(int x, int y)
        {
            return Observable.Return(PlusTwoNumber(x, y));
        }

使用SelectMany可以很方便的实现诸如在一个异步方法中调用另外一个异步方法的功能。

public static void ObservableSelectMany(int x, int y) 
{ 
         PlusTwoNumberStartAsync(x, y).SelectMany(aPlusB => MultiplyByFiveAsync(aPlusB)).Subscribe(Console.WriteLine); 
} 

private static IObservable<int> MultiplyByFiveAsync(int x) 
{ 
        return Observable.Return(MultiplyByFive(x)); 
} 

private static int MultiplyByFive(int x) 
{ 
       Thread.Sleep(5000); 
       return x * 5; 
}

完整代码如下:

// -----------------------------------------------------------------------
// <copyright file="RxAsyncCall.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace RxPractice
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reactive.Linq;
    using System.Threading;

    /// <summary>
    /// 异步调用
    /// </summary>
    public class RxAsyncCall
    {
        public static void ObservableStart(int x, int y)
        {
            PlusTwoNumberStartAsync(x, y).Subscribe(Console.WriteLine);
        }

        public static void ObservableReturn(int x, int y)
        {
            PlusTwoNumberReturnAsync(x, y).Subscribe(Console.WriteLine);
        }

        public static void ObservableSelectMany(int x, int y)
        {
            PlusTwoNumberStartAsync(x, y).SelectMany(aPlusB => MultiplyByFiveAsync(aPlusB)).Subscribe(Console.WriteLine);
        }


        private static IObservable<int> PlusTwoNumberStartAsync(int x, int y)
        {
            return Observable.Start(() => PlusTwoNumber(x, y));
        }

        private static int PlusTwoNumber(int x, int y)
        {
            Thread.Sleep(2000);
            return x + y;
        }

        private static IObservable<int> MultiplyByFiveAsync(int x)
        {
            return Observable.Return(MultiplyByFive(x));
        }


        private static int MultiplyByFive(int x)
        {
            Thread.Sleep(5000);
            return x * 5;
        }

        private static IObservable<int> PlusTwoNumberReturnAsync(int x, int y)
        {
            return Observable.Return(PlusTwoNumber(x, y));
        }
    }
}
Implementing the GeoCoordinateWatcher as a Reactive Service

Using Reactive Extensions for Streaming Data from Database

Bing it on, Reactive Extensions! – Story, code and slides

IntroToRx.com is the online resource for getting started with the  Reactive Extensions to .Net

http://rxdemo.codeplex.com/

http://blog.csdn.net/fangxinggood/article/details/7381619

Reactive Programming For .NET And C# Developers - An Introduction To IEnumerable, IQueryable, IObservable & IQbservable

本文来自云栖社区合作伙伴“doNET跨平台”,了解相关信息可以关注“opendotnet”微信公众号

目录
相关文章
|
16天前
|
JavaScript 内存技术
Vue 安装vue-element-admin启动报错error:0308010C:digital envelope routines::unsupported
Vue 安装vue-element-admin启动报错error:0308010C:digital envelope routines::unsupported
|
1天前
|
JavaScript
vue3的警告问题 [Vue warn]: inject() can only be used inside setup() or functional components. 怎么解决?
vue3的警告问题 [Vue warn]: inject() can only be used inside setup() or functional components. 怎么解决?
12 5
|
4月前
Uncaught runtime errors: × ERROR Cannot read properties of undefined (reading ‘ vue2&vue-router兼容性问题
Uncaught runtime errors: × ERROR Cannot read properties of undefined (reading ‘ vue2&vue-router兼容性问题
135 0
|
25天前
Classic mode for store/ is deprecated and will be removed in Nuxt 3
Classic mode for store/ is deprecated and will be removed in Nuxt 3
26 0
|
1月前
|
存储 JavaScript 容器
Vue3通信方式之ref、$parent、provide、inject、pinia和slot
Vue3通信方式之ref、$parent、provide、inject、pinia和slot
13 0
|
4月前
|
编解码 JavaScript 编译器
【Vue warn】If this is a native custom element, make sure to exclude it from component resolution ……
【Vue warn】If this is a native custom element, make sure to exclude it from component resolution ……
|
7月前
|
JavaScript API
vue组件通讯之provide / inject
vue组件通讯之provide / inject
27 0
|
8月前
|
JavaScript 前端开发
error: The “xx“ component has been registered but not used (vue/no-unused-components) at
报错原因:因为搭建的vue项目选择了eslint校验规范->就是你定义了某个变量,但是你没有使用它.eslint规范就是你要么不定义,要么定义了就一定得用.
362 0
|
10月前
|
JavaScript 前端开发 API
Vue3 —— 常用 Composition API(零)(setup函数、ref函数、reactive函数、响应式、reactive对比ref)
Vue3 —— 常用 Composition API(零)(setup函数、ref函数、reactive函数、响应式、reactive对比ref)
|
存储 测试技术 API
vue3源码分析——实现组件通信provide,inject
问题解决: 问题1和问题2都很好解决,对外导出函数,传递对应的参数,只是数据存储在哪里的问题,经过仔细的思考,会发现,组件的数据是需要进行共享的,父组件存入的数据,里面的所有子组件和孙子组件都可以共享,那么存储在实例上,是不是一个不错的选择呢? inject 是获取父级组件的数据,那么在实列上还需要传入parent