Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

374 views

Published on

介紹近年在各語言都火熱的 Reactive Programming 架構與其特性,並透過 Rx.Net 展示如何實作於.Net Core的開發中,帶來更多架構的選擇與彈性。

Lab:
https://github.com/blackie1019/reactive-progamming-dot-net

Published in: Software
  • Be the first to comment

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

  1. 1. .NET Conf Learn. Imagine. Build. .NET Conf 響應式程式開發之 .NET Core 應用 (Reactive Programming with .NET Core) Blackie Tsai
  2. 2. .NET Conf 這場不會深入的討論如何把 Code 寫好寫滿
  3. 3. .NET Conf 主要是介紹 Reactive Programming
  4. 4. .NET Conf 並透過實際展示,認識 Reactive Programming
  5. 5. .NET Conf Reactive Programming 是一種編碼風格
  6. 6. .NET Conf 讓我們用一種更合適的方式 撰寫某些情境的程式
  7. 7. .NET Conf
  8. 8. .NET Conf
  9. 9. .NET Conf
  10. 10. .NET Conf
  11. 11. .NET Conf 介紹 Reactive Programming 展示 更多 Reactive 上手 Rx.NET 介紹 Reactive Extensions
  12. 12. .NET Conf (https://blackie1019.github.io/) .NET JAVASCRIPT
  13. 13. .NET Conf 什麼是 Reactive Programming?
  14. 14. .NET Conf
  15. 15. .NET Conf
  16. 16. .NET Conf
  17. 17. .NET Conf
  18. 18. .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); 以下程式結果為何?
  19. 19. .NET Conf before: the value of c is 5 after: the value of c is 5 正常來說…
  20. 20. .NET Conf before: the value of c is 5 after: the value of c is 9 但有時候我們期望能出現以下結果
  21. 21. .NET Conf
  22. 22. .NET Conf
  23. 23. .NET Conf
  24. 24. .NET Conf Reactive Programming 是 學習如何與周遭事件進行非 同步開發
  25. 25. .NET Conf Reactive Programming 是 學習將事件當作資料流 (Stream)進行操作
  26. 26. .NET Conf
  27. 27. .NET Conf
  28. 28. .NET Conf 當到此資料流結束 發生一個錯誤 某一個click event 時間
  29. 29. .NET Conf Again, 什麼是 Reactive Programming?
  30. 30. .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
  31. 31. .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
  32. 32. .NET Conf 為什麼要考慮使用Reactive Programming?
  33. 33. .NET Conf
  34. 34. .NET Conf https://www.reactivemanifesto.org/ Message Driven 透過非同步訊息溝通達到模組隔離 與委託處理 Responsive 系統在任何情況下,都能及時響應 Resilient 系統在異常時,具有容錯性 Elastic 系統能依據需求彈性拓展
  35. 35. .NET Conf 再繼續講主題前,讓我們離 題一下認識 Functional Programming
  36. 36. .NET Conf
  37. 37. .NET Conf
  38. 38. .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);
  39. 39. .NET Conf [1, 4, 9].map(Math.sqrt).map( function(x){ return x + 1 } ); => [2, 3, 4] JavaScript 範例
  40. 40. .NET Conf Object-Oriented Programming Functional Programming 程式開發抽象的核心 資料結構 函式 資料與操作的關係 緊耦合 鬆耦合 溝通方式 物件(Objects) 透過介面 函式(Functions)透過協定 是否有狀態 有狀態(Stateful) 無狀態(Stateless) 回應值是否透明、可預測 回應值由輸入與狀態確定 回應值總是由輸入確定 核心活動 通過向其添加新方法來組合新的物件對象和 擴展現有物件對象 撰寫更多新函式
  41. 41. .NET Conf
  42. 42. .NET Conf • 我已經在使用aync與await做開發了,沒有必要採 用其他框架來達到異部處理的需求了 • 因為牽涉到開發架構與框架,一旦採用了就沒有回 頭路了 • 因為牽涉到開發架構與框架,一旦採用了就沒有回 頭路了
  43. 43. .NET Conf 介紹 Reactive Extensions
  44. 44. .NET Conf http://reactivex.io/
  45. 45. .NET Conf Rx 是結合 觀察器模式, 迭代器模式與函 數編程的函式庫、框架與開 發方式。 觀察器模式, 迭代器模式與函 數編程
  46. 46. .NET Conf
  47. 47. .NET Conf
  48. 48. .NET Conf
  49. 49. .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
  50. 50. .NET Conf
  51. 51. .NET Conf https://github.com/Reactive-Extensions/Rx.NET PM> Install-Package System.Reactive 或 > dotnet add package System.Reactive --version 4.0.0-preview00001
  52. 52. .NET Conf Lab_00 - Reactive Programming Example Talk is cheap. Show me the code !
  53. 53. .NET Conf Rx 是結合 觀察器模式, 迭代器模式與函 數編程的函式庫、框架與開 發方式。 Rx = Observables + LINQ + Schedulers
  54. 54. .NET Conf 大话设计模式之观察者模式
  55. 55. .NET Conf
  56. 56. .NET Conf
  57. 57. .NET Conf
  58. 58. .NET Conf
  59. 59. .NET Conf
  60. 60. .NET Conf 大话设计模式 - 迭代器模式之看芒果台還是央视nie?
  61. 61. .NET Conf L IN Q
  62. 62. .NET Conf
  63. 63. .NET Conf
  64. 64. .NET Conf Lab_01 – HelloWorld on Rx.NET Talk is cheap. Show me the code!
  65. 65. .NET Conf Rx.NET 核心觀念
  66. 66. .NET Conf 拉取(PULL)
  67. 67. .NET Conf 推播(PUSH)
  68. 68. .NET Conf
  69. 69. .NET Conf
  70. 70. .NET Conf Rx.NET 主要資料結構
  71. 71. .NET Conf
  72. 72. .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
  73. 73. .NET Conf // System.Runtime.dll public interface IObservable<T> { IDisposable Subscribe(IObserver<T> observer); } IObservable
  74. 74. .NET Conf // System.Runtime.dll public interface IObserver<T> { void OnNext(T value); void OnCompleted(); void OnError(Exception exception); } IObserver
  75. 75. .NET Conf // System.Runtime.dll public interface IDisposable { void Dispose(); } IDisposable
  76. 76. .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
  77. 77. .NET Conf Lab_02 - IObservable and IObserver with Rx.NET Talk is cheap. Show me the code !
  78. 78. .NET Conf Rx Asynchronous programming model Events Delegates Tasks IEnumerable<T>
  79. 79. .NET Conf Rx.NET 方法
  80. 80. .NET Conf
  81. 81. .NET Conf
  82. 82. .NET Conf
  83. 83. .NET Conf Rx.NET 常見操作子
  84. 84. .NET Conf
  85. 85. .NET Conf
  86. 86. .NET Conf
  87. 87. .NET Conf
  88. 88. .NET Conf
  89. 89. .NET Conf
  90. 90. .NET Conf
  91. 91. .NET Conf http://rxmarbles.com/
  92. 92. .NET Conf http://rxwiki.wikidot.com/101samples
  93. 93. .NET Conf • Lab_03 - Advance with Rx Talk is cheap. Show me the code !
  94. 94. .NET Conf
  95. 95. .NET Conf
  96. 96. .NET Conf
  97. 97. .NET Conf Lab_04 - Stock Monitoring with Rx.NET Lab_05 - MQTT with Rx.NET Lab_06 - Xamarin with Rx.NET Showcases
  98. 98. .NET Conf https://reactiveui.net/
  99. 99. .NET Conf https://github.com/Reactive-Extensions/RxJS Draggable UI Auto Completed 出處 :2017 iT 邦幫忙鐵人賽 : 30 天精通 RxJS
  100. 100. .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.”
  101. 101. .NET Conf 常見疑問
  102. 102. .NET Conf 或是 必定 函数式反应型编程(FRP) —— 实 时互动应用开发的新思路
  103. 103. .NET Conf
  104. 104. .NET Conf
  105. 105. .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
  106. 106. .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
  107. 107. .NET Conf fb.com/Study4.twfb.com/groups/216312591822635 Study4.TW
  108. 108. .NET Conf
  109. 109. .NET Conf

×