Deep understanding of C#rx's main interface

Source: Internet
Author: User
This article mainly introduces the main interface of C # Rx in-depth understanding of the relevant information, the need for friends can refer to the following

C # Rx's main interface in-depth understanding

The Iobservable/iobserver interface is available in the. NET Framework 4.0 base class libraries, and they are included in packages that can be installed in. NET 3.5,silverlight 3 and 4, as well as in JavaScript.

Iobservable/iobserver

Rx exposes asynchronous and event-based data sources as push-based observable sequences, abstracted by the new iobservable interface in the. NET Framework 4.0. This iobservable interface is used for pull-based, enumerable collections of the familiar IEnumerable interfaces of the dual. It represents a data source that can be observed, which means it can send data to anyone who is interested. It maintains a list of dependent iobserver implementations that represent such listeners of interest, and automatically notifies them of any state change.

The implementation of the IObservable interface can be considered a collection of elements of type T. Therefore, iobservable can be treated as a collection of integers, where integers are pushed to the observer of the subscription

As described in "What is Rx," the other half of the push model is represented by the IObserver interface, which represents an observer who is interested in registering by subscribing. The item is then delivered to the observer from the observable sequence of its subscription.

In order to receive notifications from the observable collection, you use the IObservable subscribe method to pass the IObserver object to it. In return for this observer, the Subscribe method returns a IDisposable object as a handle to the subscription. This allows you to clean up your subscription when you are done. Call Dispose on this object to detach the observer from the source so that the notification is no longer delivered. As you can infer, in Rx, it doesn't need to be like. NET event model, and explicitly unsubscribe from events.

The Observer supports three release events, which are reflected by the method of the interface. When observable data sources have available data, OnNext can be called 0 or more times. For example, an observable data source for mouse movement events can emit a point object each time the mouse moves. The other two methods are used to indicate completion or error.

The Iobservable/iobserver interface is listed below.


Public interface iobservable<out t> {   IDisposable Subscribe (iobserver<t> observer), public interface Iobserver<in t> {   void oncompleted ();//Notifies the Observer that the source have finished sending messages.
  
   void OnError (Exception error); Notifies the observer about any exception or error.  void OnNext (T value);     Pushes the next data value from the source to the observer.}
  

RX also provides the subscribe extension method so that you can avoid implementing the IObserver interface yourself. For each published event (onnext,onerror,oncompleted) of an observable sequence, you can specify the delegate that will be invoked, as shown in the following example. If you do not specify an action for an event, the default behavior occurs.


Iobservable<int> Source = Observable.range (1, 5); Creates an observable sequence of 5 integers, starting from 1IDisposable subscription = source. Subscribe (              x = Console.WriteLine ("OnNext: {0}", x),//prints out the value being pushed              ex = Console.Write Line ("OnError: {0}", ex. Message),              () = Console.WriteLine ("oncompleted"));

You can view an observable sequence, such as a sequence of mouse hover events, as a normal collection. Therefore, you can write LINQ queries on collections to perform operations such as filtering, grouping, compositing, and so on. To make observable sequences more useful, the RX assembly provides many factory LINQ operators, so you do not need to implement any of these yourself. This will observe the sequence topic in a query that uses the LINQ operator.

Warning:

You do not need to implement the Iobservable/iobserver interface yourself. RX provides you with an internal implementation of these interfaces and exposes them through the various extension methods provided by the observable and observer types. For more information, see Create and query an observable sequence topic

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.