reactive extensions rx
play

Reactive Extensions (Rx) Your prescription to cure event processing - PowerPoint PPT Presentation

Reactive Extensions (Rx) Your prescription to cure event processing blues Bart J.F. De Smet Software Development Engineer bartde@microsoft.com Why should I care? GPS RSS feeds Social media Server management Event Processing Systems


  1. Reactive Extensions (Rx) Your prescription to cure event processing blues Bart J.F. De Smet Software Development Engineer bartde@microsoft.com

  2. Why should I care? GPS RSS feeds Social media Server management

  3. Event Processing Systems �� ����� � ������� �� ◦ ����� � ������� Way simpler with Rx �������������������� ������������������������������ �������������������� �������������������� ���������� ���������� ���������� ���������������������� ���������������������� ���������������������� ����������������������� ������ � � ����� ����� ����� ��������������� ��������������� ��������������� �������������������������� ����������� �������������������� ����������� ��������� ���������� ��������� � � � Queries! LINQ! � .NET 3.5 SP1, 4.0, and 4.5 � Silverlight 4, and 5 � Windows Phone 7 and 7.5 � JavaScript (RxJS) Download at MSDN Data Developer Center or use NuGet

  4. Observable Sequences Mathematical Dual of IEnumerable<T> interface IObservable<out T> { IDisposable Subscribe(IObserver<T> observer); } interface IObserver<in T> { void On Next(T value); void On Error(Exception ex); void On Completed(); }

  5. Push-Based Data Retrieval Application Interactive MoveNext Reactive Got next? OnNext Have next! IObservable<T> IEnumerable<T> IObserver<T> IEnumerator<T> �����������

  6. DEMO The IObservable<T> interface

  7. Creating Observable Sequences OnNext * [ OnError | OnCompleted ] Observable.Never<int>() ����������� Observable.Empty<int>() ���������� Observable.Return<int>(42) ����������� Observable.Throw<int>(ex)

  8. Generator Functions var e = new IEnumerable<int> { var o = Observable.Generate( for (int i = 0; 0, i < 10; i => i < 10, i++) i => i + 1, yield return i * i; i => i * i }; ); ������������ ����������� o.Subscribe(x => { foreach (var x in e) { Console.WriteLine(x); Console.WriteLine(x); } });

  9. The Create operator Observable<int> o = Observable.Create<int>(observer => { // Assume we introduce concurrency (see later)… observer.OnNext(42); observer.OnCompleted(); return () => { /* unsubscribe action */ }; }); IDisposable subscription = o.Subscribe( onNext: x => { Console.WriteLine("Next: " + x); }, onError: ex => { Console.WriteLine("Oops: " + ex); }, onCompleted: () => { Console.WriteLine("Done"); } ); ���������������� �������������������� ������������� �������������� ������������������������ ���������������� ������������������������������������

  10. The Create operator Observable<int> o = Observable.Create<int>(observer => { // Assume we introduce concurrency (see later)… observer.OnNext(42); observer.OnCompleted(); return () => { /* unsubscribe action */ }; }); IDisposable subscription = o.Subscribe( onNext: x => { Console.WriteLine("Next: " + x); }, onError: ex => { Console.WriteLine("Oops: " + ex); }, onCompleted: () => { Console.WriteLine("Done"); } ); Thread.Sleep(30000); ���

  11. The Create operator Observable<int> o = Observable.Create<int>(observer => { // Assume we introduce concurrency (see later)… observer.OnNext(42); observer.OnCompleted(); return () => { /* unsubscribe action */ }; }); IDisposable subscription = o.Subscribe( onNext: x => { Console.WriteLine("Next: " + x); }, onError: ex => { Console.WriteLine("Oops: " + ex); }, onCompleted: () => { Console.WriteLine("Done"); } ); Thread.Sleep(30000); ���

  12. The Create operator Observable<int> o = Observable.Create<int>(observer => { // Assume we introduce concurrency (see later)… observer.OnNext(42); observer.OnCompleted(); return () => { /* unsubscribe action */ }; }); IDisposable subscription = o.Subscribe( onNext: x => { Console.WriteLine("Next: " + x); }, onError: ex => { Console.WriteLine("Oops: " + ex); }, onCompleted: () => { Console.WriteLine("Done"); } ); Thread.Sleep(30000); ��

  13. The Create operator Observable<int> o = Observable.Create<int>(observer => { // Assume we introduce concurrency (see later)… observer.OnNext(42); observer.OnCompleted(); return () => { /* unsubscribe action */ }; }); ����������� ������� IDisposable subscription = o.Subscribe( onNext: x => { Console.WriteLine("Next: " + x); }, onError: ex => { Console.WriteLine("Oops: " + ex); }, onCompleted: () => { Console.WriteLine("Done"); } ); Thread.Sleep(30000);

  14. DEMO Creating observable sequences

  15. The Trouble with .NET Events ������������������� ������������������ ��������������� ���������� ���������������� ���������� ���������� ���������� ���� ���� �� � ���������������� �� ���������������� � � � � � ���� ��!�"���������������#�������� $% ��!��"������������ ����������������& & & &� � � � �'�(#��������#���)�'� #��������������������

  16. Observable Sequences are First-Class Objects �'(���������'�������� $�������"�%�����&����� �*+�����+!� �*+�����+!�, �*+�����+!� �*+�����+!� , , ,-���� -����� -���� -���� � � ���������� �� � *+�����+!��.���/���� *+�����+!��.���/���������� " ��������� " �% *+�����+!��.���/���� *+�����+!��.���/���� ��� ��!������������������ �0#���� �0#���� �0#����1�� ���1���� ���1����� �0#���� � � �% ������"������������� ��� ��+����1���������!������2�+����+��3�% ��+����1�����4��1��� �4��1��� �4��1��� �4��1����� �� �� ��% #�������������������)

  17. Other Conversions with IObservable<T> � Asynchronous Programming Model (APM) � Legacy support; prefer going through Task<T> .���,������*+�����+!�,���������1���5�6�� �� *+�����+!��.���5�6��-������ �.���5�6��-�����������7����8��1���� �.���5�6��-������ �.���5�6��-������ ����/��8��1����% � Task<T> � Way to represent single-value asynchrony 9��",��������#��!9��" �� (�+8!�����4�(�!���2�����9��"5�6������� � � � �9�*+�����+!� 9�*+�����+!� 9�*+�����+!��� 9�*+�����+!� �� �� ��% � IEnumerable<T> � Pull-to-push conversion But where does the enumeration happen? �*+�����+!�,��������9�:��� �� /������+!��;�����<���<� � � �9�*+�����+!� � 9�*+�����+!� 9�*+�����+!� 9�*+�����+!��� �� �� ��%

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend