Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[Study4.TW .NET Conf 2017] 響應式程式開發之 .NET Core 應用

[Study4.TW .NET Conf 2017] 響應式程式開發之 .NET Core 應用

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

Blackie Tsai

October 13, 2017
Tweet

More Decks by Blackie Tsai

Other Decks in Programming

Transcript

  1. .NET Conf Learn. Imagine. Build. .NET Conf 響應式程式開發之 .NET Core

    應用 (Reactive Programming with .NET Core) Blackie Tsai
  2. .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); 以下程式結果為何?
  3. .NET Conf before: the value of c is 5 after:

    the value of c is 5 正常來說…
  4. .NET Conf before: the value of c is 5 after:

    the value of c is 9 但有時候我們期望能出現以下結果
  5. .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
  6. .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
  7. .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);
  8. .NET Conf Object-Oriented Programming Functional Programming 程式開發抽象的核心 資料結構 函式 資料與操作的關係

    緊耦合 鬆耦合 溝通方式 物件(Objects) 透過介面 函式(Functions)透過協定 是否有狀態 有狀態(Stateful) 無狀態(Stateless) 回應值是否透明、可預測 回應值由輸入與狀態確定 回應值總是由輸入確定 核心活動 通過向其添加新方法來組合新的物件對象和 擴展現有物件對象 撰寫更多新函式
  9. .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
  10. .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
  11. .NET Conf // System.Runtime.dll public interface IObserver<T> { void OnNext(T

    value); void OnCompleted(); void OnError(Exception exception); } IObserver
  12. .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
  13. .NET Conf Lab_04 - Stock Monitoring with Rx.NET Lab_05 -

    MQTT with Rx.NET Lab_06 - Xamarin with Rx.NET Showcases
  14. .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.”
  15. .NET Conf Intro to Rx 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 [YouTube] Playlist - Reactive Extensions for .NET (Rx.NET)
  16. .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
  17. .NET Conf 商業思維 出差意願 (非必備) 多語溝通 (非必備) 領導/溝通 能力 高流量網商

    HA/雲架構 網站、資料庫 效能校調 Infra 知識 系統分析 系統設計 DevOps 經驗