.NET Conf
Learn. Imagine. Build.
.NET Conf
響應式程式開發之 .NET Core 應用
(Reactive Programming with .NET Core)
Blackie Tsai
.NET Conf
這場不會深入的討論如何把
Code 寫好寫滿
.NET Conf
主要是介紹
Reactive Programming
.NET Conf
並透過實際展示,認識
Reactive Programming
.NET Conf
Reactive Programming
是一種編碼風格
.NET Conf
讓我們用一種更合適的方式
撰寫某些情境的程式
.NET Conf
.NET Conf
.NET Conf
.NET Conf
.NET Conf
介紹
Reactive Programming
展示
更多 Reactive
上手
Rx.NET
介紹
Reactive Extensions
.NET Conf
(https://blackie1019.github.io/)
.NET JAVASCRIPT
.NET Conf
什麼是
Reactive Programming?
.NET Conf
.NET Conf
.NET Conf
.NET Conf
.NET Conf
int a = 2;
int b = 3;
int c = a + b;
Console. WriteLine("before: the value of c is {0}",c);
a=2;
b=7;
Console. WriteLine("after: the value of c is {0}",c);
以下程式結果為何?
.NET Conf
before: the value of c is 5
after: the value of c is 5
正常來說…
.NET Conf
before: the value of c is 5
after: the value of c is 9
但有時候我們期望能出現以下結果
.NET Conf
.NET Conf
.NET Conf
.NET Conf
Reactive Programming 是
學習如何與周遭事件進行非
同步開發
.NET Conf
Reactive Programming 是
學習將事件當作資料流
(Stream)進行操作
.NET Conf
.NET Conf
.NET Conf
當到此資料流結束
發生一個錯誤
某一個click event
時間
.NET Conf
Again, 什麼是
Reactive Programming?
.NET Conf
Console.Write("Press input A:");
var a = Convert.ToInt32(Console.ReadLine());
Console.Write("Press input B:");
var b = Convert.ToInt32(Console.ReadLine());
var c = a+b;
Console.WriteLine("before: the value of c is {0}",c);
Console.Write("Press input B again:");
b = Convert.ToInt32(Console.ReadLine());
c = a+b;
Console.WriteLine("after: the value of c is {0}",c);
Imperative programming
.NET Conf
while (true)
{
Console.Write("Press input A:");
var a = Convert.ToInt32(Console.ReadLine());
Console.Write("Press input B:");
var subscription = Observable
.FromAsync(() => Console.In.ReadLineAsync())
.Subscribe(b => Console.WriteLine("the value of c is
{0}",a+Convert.ToInt32(b)));
Console.Write("enter symbol (or x to exit): ");
…
}
Reactive programming
.NET Conf
為什麼要考慮使用Reactive
Programming?
.NET Conf
.NET Conf
https://www.reactivemanifesto.org/
Message Driven
透過非同步訊息溝通達到模組隔離
與委託處理
Responsive
系統在任何情況下,都能及時響應
Resilient
系統在異常時,具有容錯性
Elastic
系統能依據需求彈性拓展
.NET Conf
再繼續講主題前,讓我們離
題一下認識 Functional
Programming
.NET Conf
.NET Conf
.NET Conf
C# 範例
Building building = SomeQueryThatShouldReturnBuilding();
var phoneNumber = null;
// Object-Oriented Programming
if(building != null){
if(building.Manager.ContactInfo != null){
if(building.Manager.ContactInfo.PhoneNumber != null){
phoneNumber = building.Manager.ContactInfo.PhoneNumber;
}
}
}
// Functional Programming
phoneNumber = SomeQueryThatShouldReturnBuilding()
.With(b=>b.Manager)
.With(m=>m.ContactInfo)
.With(c=>c.PhoneNumber);
.NET Conf
[1, 4, 9].map(Math.sqrt).map(
function(x){
return x + 1
}
);
=> [2, 3, 4]
JavaScript 範例
.NET Conf
Object-Oriented Programming Functional Programming
程式開發抽象的核心 資料結構 函式
資料與操作的關係 緊耦合 鬆耦合
溝通方式 物件(Objects) 透過介面 函式(Functions)透過協定
是否有狀態 有狀態(Stateful) 無狀態(Stateless)
回應值是否透明、可預測 回應值由輸入與狀態確定 回應值總是由輸入確定
核心活動 通過向其添加新方法來組合新的物件對象和
擴展現有物件對象
撰寫更多新函式
.NET Conf
.NET Conf
• 我已經在使用aync與await做開發了,沒有必要採
用其他框架來達到異部處理的需求了
• 因為牽涉到開發架構與框架,一旦採用了就沒有回
頭路了
• 因為牽涉到開發架構與框架,一旦採用了就沒有回
頭路了
.NET Conf
介紹 Reactive Extensions
.NET Conf
http://reactivex.io/
.NET Conf
Rx 是結合
觀察器模式, 迭代器模式與函
數編程的函式庫、框架與開
發方式。
觀察器模式, 迭代器模式與函
數編程
.NET Conf
.NET Conf
.NET Conf
.NET Conf
RxJava
RxJS
Rx.NET
UniRx
RxScala
RxClojure
RxCpp
RxLua
Rx.rb
RxPY
RxGo
RxGroovy
RxJRuby
RxKotlin
RxSwift
RxPHP
reaxive
RxDart
RX for Xamarin(Rx.NET)
RxAndroid
RxCocoa
RxNetty
.NET Conf
.NET Conf
https://github.com/Reactive-Extensions/Rx.NET
PM> Install-Package System.Reactive
或
> dotnet add package System.Reactive --version 4.0.0-preview00001
.NET Conf
Lab_00 - Reactive Programming Example
Talk is cheap. Show me the code !
.NET Conf
Rx 是結合
觀察器模式, 迭代器模式與函
數編程的函式庫、框架與開
發方式。
Rx = Observables + LINQ + Schedulers
.NET Conf
大话设计模式之观察者模式
.NET Conf
.NET Conf
.NET Conf
.NET Conf
.NET Conf
.NET Conf
大话设计模式 - 迭代器模式之看芒果台還是央视nie?
.NET Conf
L IN Q
.NET Conf
.NET Conf
.NET Conf
Lab_01 – HelloWorld on Rx.NET
Talk is cheap. Show me the code!
.NET Conf
Rx.NET 核心觀念
.NET Conf
拉取(PULL)
.NET Conf
推播(PUSH)
.NET Conf
.NET Conf
.NET Conf
Rx.NET 主要資料結構
.NET Conf
.NET Conf
IObservable<string> subject = new [] { "Jordan", "Kobe", "James"}.ToObservable();
IObservable<string> subject = Observable.FromAsync (() =>
Console.In.ReadLineAsync ());
IObservable<int> subject = Observable.Range(5, 8);
IObservable<int> subject =
from i in Observable.Range(1, 100)
from j in Observable.Range(1, 100)
from k in Observable.Range(1, 100)
where k * k == i * i + j * j
select new { a = i, b = j, c = k };
IObservable<long> subject = Observable.Timer(TimeSpan.FromSeconds(3));
Subject
.NET Conf
// System.Runtime.dll
public interface IObservable<T>
{
IDisposable Subscribe(IObserver<T> observer);
}
IObservable
.NET Conf
// System.Runtime.dll
public interface IObserver<T>
{
void OnNext(T value);
void OnCompleted();
void OnError(Exception exception);
}
IObserver
.NET Conf
// System.Runtime.dll
public interface IDisposable
{
void Dispose();
}
IDisposable
.NET Conf
// System.Reactive.Interfaces.dll
public interface IScheduler
{
DateTimeOffset Now { get; }
IDisposable Schedule<TState>(TState state, Func<IScheduler, TState,
IDisposable> action);
IDisposable Schedule<TState>(TState state, TimeSpan dueTime,
Func<IScheduler, TState, IDisposable> action);
IDisposable Schedule<TState>(TState state, DateTimeOffset dueTime,
Func<IScheduler, TState, IDisposable> action);
}
ISchedule
.NET Conf
Lab_02 - IObservable and IObserver with Rx.NET
Talk is cheap. Show me the code !
.NET Conf
Rx
Asynchronous
programming
model
Events
Delegates
Tasks
IEnumerable<T>
.NET Conf
Rx.NET 方法
.NET Conf
.NET Conf
.NET Conf
.NET Conf
Rx.NET 常見操作子
.NET Conf
.NET Conf
.NET Conf
.NET Conf
.NET Conf
.NET Conf
.NET Conf
.NET Conf
http://rxmarbles.com/
.NET Conf
http://rxwiki.wikidot.com/101samples
.NET Conf
• Lab_03 - Advance with Rx
Talk is cheap. Show me the code !
.NET Conf
.NET Conf
.NET Conf
.NET Conf
Lab_04 - Stock Monitoring with Rx.NET
Lab_05 - MQTT with Rx.NET
Lab_06 - Xamarin with Rx.NET
Showcases
.NET Conf
https://reactiveui.net/
.NET Conf
https://github.com/Reactive-Extensions/RxJS
Draggable UI
Auto Completed
出處 :2017 iT 邦幫忙鐵人賽 : 30 天精通 RxJS
.NET Conf
“Do not forget nor dwell on the past, but do forgive it. Be aware
of the future but do no fear or worry about it. Focus on the
present moment, and that moment alone.”
.NET Conf
常見疑問
.NET Conf
或是
必定
函数式反应型编程(FRP) —— 实
时互动应用开发的新思路
.NET Conf
.NET Conf
.NET Conf
ReactiveX Official Site
Intro to Rx
Interactive diagrams of Rx Observables
Cloud-Scale Event Processing with the Reactive Extensions
(Rx) - Bart De Smet
The introduction to Reactive Programming you've been
missing
A Playful Introduction to Rx by Erik Meijer
.NET Conf
Rx.NET in Action: With examples in C#
.NET Design Patterns
Introduction to Rx: A step by step guide to the Reactive
Extensions to .NET
.NET Conf
fb.com/Study4.twfb.com/groups/216312591822635 Study4.TW
.NET Conf
.NET Conf

響應式程式開發之 .NET Core 應用 

Editor's Notes

  • #12 Opening & Personal Introduction - 3 mins Reactive Programming Introduction - 8 mins Reactive Extensions Introduction – 9 mins .NET Core with Rx.NET - 10 mins Adv .NET Core with Rx.NET – 15 mins More Reactive - 2 mins Summary - 3 mins Others - 1 mins
  • #15 Gérard Philippe Berry 是法籍的電腦開學家,由他所提出的 Reactive Programming 是一種開發的編碼風格與架構,可以讓我們抱持與環境的互動,並將結果交由與其互動的環境來決定,而非程序本身。 這邊他也同時將所有程序按照其互動,分為了三種類型 Transformational program Interactive program Reactive program
  • #16 給定的一組輸入並直接得到結果
  • #17 以自己的速度與環境或其他程序互動
  • #18 保持與環境的持續互動,但速度取決於環境而不是程序本身。
  • #19 有獎徵答1
  • #29 http://blog.kitchenet.tn/what-is-reactive-programming/
  • #35 響應式系統具備靈活性,鬆耦合和可擴展性。這使得它們更容易去開發、修改。而對 Error 與 Exception 有更大的容忍,並且當狀況發生時它們會更優雅地應對而不是讓直接系統崩潰。反應式系統有著高度的響應性,並會給用戶有效的交互反饋。 響應式系统是: Responsive 響應式(reacts to events at the speed of environment) 系統在任何情況下,都能及時響應,在可靠的時間內提供訊息且有穩定品質的服務。 Resilient 韌性、堅固, 具有容錯性(Fault-tolerant) 系統在失敗時,仍然保有 Responsive 特性。這是由 replication (複製), containment (容忍), isolation (隔離)以及 delegation (委託)的方式達成的。系統的任何組件發生問題時,可以隔離此部份,並獨立恢復,過程中不會影響整個系統。 Elastic 彈性(Easy Scale up/down and out) 即時量測效能,可透過演算法增加或減少服務的資源分配,滿足不同工作負載量的系統要求。 Message Driven 訊息驅動(modular, pipelined, asynchronous) 組件之間透過非同步訊息傳遞方式互相溝通,用以確保 loose coupling (鬆耦合), isolation (隔離), location transparency (與區域無關)的特性。還能提供錯誤訊息的委託處理機制。
  • #38 Anders Hejlsberg(安德斯·海爾斯伯格) 丹麥籍的開發教父,Pascal編譯器的主要開發人員,也是Delph與C#之父進入微軟公後,先後設計出了了Visual J ++,Net,C#和TypeScript。 可以說他開發出的軟體和發明的程式語言影響全世界程序員。目前,他是C#語言的首席架構師和TypeScript的核心開發者與TypeScript開源項目的重要領導人 functional Programming 允許開發者描述什麼是他們想要程序去做的,而不是強迫他們描述程序該如何去做
  • #41 資料(data) 與操作(operations)
  • #45 The .NET Foundation is an independent organization, incorporated on March 31, 2014,[1] by Microsoft, to improve open-source software development and collaboration around the .NET Framework.[4] It was launched at the annual Build 2014 conference held by Microsoft.[5] 
  • #46 結合觀察者模式(Observer Pattern), 迭代器模式(Iterator Pattern)與函數編程(Functional Programming)的函式庫、框架與開發方式。
  • #47 依賴異步訊息傳遞去建立不同組件間的邊界,並以此確保鬆耦合、可隔離、位置透明和提供一種方式可將錯誤用訊息的方式委託至外部通知。 通過塑造和監視系統中的訊息佇列在必要時使用 Backpressure,顯式消息傳遞的使用可以實現負載管理、可擴展性和流程控制。 在通訊中使用位置透明的訊息傳遞使得管理集群和單個主機擁有相同的結構和語義 non-blocking 的通信允許接收者只在活動時消耗資源,可以減少系統的開銷。 Backpressure 指的是在 Buffer 有上限的系统中,Buffer 溢出的现象;它的应对措施只有一个:丢弃新事件。
  • #52 Package Manager Install-Package System.Reactive -Version 4.0.0-preview00001 .NET CLI dotnet add package System.Reactive --version 4.0.0-preview00001 System.Reactive.Core:  Is the heart of Rx and had extension methods for subscribing to subject. It has static class Observer to create observers. It also provides synchronization and scheduling services. System.Reactive.Interfaces: This provides the interfaces for for event pattern which subject can implement to raise events when data is available. It also has the scheduler interfaces and queriable interfaces for Rx schedulers and LINQ support. System.Reactive.Linq: This provides the famous static class Observable to create in-memory observable sequence. This extends the basic LINQ feature to Rx. System.Reactive.PlatformServices: This extends the basic scheduling services provided in System.Reactive.Core and will replace all the scheduling service in future. Dotnet Core Version: dotnet new console --framework netcoreapp2.0 dotnet add package System.Reactive --version 4.0.0-preview00001
  • #53 展示A+B的範例
  • #54 結合觀察者模式(Observer Pattern), 迭代器模式(Iterator Pattern)與函數編程(Functional Programming)的函式庫、框架與開發方式。
  • #55 想知道一周發生什麼無腦新聞,看狂新聞就好了
  • #61  有獎徵答2 有人發現這幅圖真正的意思嗎?=>少雞8
  • #65 展示 - Array - Linq With Subscribling
  • #78 MyObserver MyObserver with Lambda MyObserver with Lambda and NewThread MyObservable EvenNumbers EvenNumbers by Rx.NET Example Odd and Even Numbers by Rx.NET Example
  • #79 Asynchronous programming model Events Delegates Tasks IEnumerable<T>
  • #98 Rx.NET in Action Demo Basic Delay Throttle Buffer Query Combine latest https://www.youtube.com/watch?v=DYEbUF4xs1Q
  • #101 Dalai Lama’s 18 Rules for Living” Expanded 不要忘記過去也不要留在過去,而是要放下。留意未來,但不要害怕也不要擔心。專注於現在的時刻,那一刻
  • #108 Others: