Monday, December 9, 2019

Buffer in Reactive C#

React is smart design pattern and quit handy for observable pattern. To reduce frequent communication Buffer is used to subscribe an IObservable. I am going to discuss a simple example with c#.net IObservable and react. Assuming that the reader have the basic idea of React. IObservable, IDisposable.
Example: Assume that there is already an implementation ReadIEventData which is IObservable .IEventData is a custom Interface which provide some data integar values.
Lets say, we have to show the event data into a chart and the amount of data is too much frequent to handle each and every data via a communication channel(i.e: tcp/ip, I2C).
To reduce the communication we want to get a bulk amount data after a certain period and then process those data into another thread.

A simple solution for that is using the Buffer for a timespan.

See the difference between two lines of code below:

myDataProvider.ReadIEventData.Finally(OnFinally).Subscribe(onNext);

Above line, subscribes the ReadIEventData and respond for every data into the method onNext.

myDataProvider.ReadIEventData.Buffer(TimeSpan.FromSeconds(2)).Finally(OnFinally).Subscribe(onNextBuffer);


Above line, subscribes the ReadIEventData  but wait for 2 seconds. Collect all the subscribed data within 2 seconds and then forward it to onNextBuffer.

So, the implementation should be like
private OnNext(IEventData singleEventData) { //Do something here }
and
private OnNextBuffer(IList bufferedEventData) { //Do something on the collection of evnetData }


-----------------------------------------------------------------------------------

Rest Service using WEB API C#

This post will show a simple example of REST service built on Micrsoft WEB API framework. WEB API is an extensible framework from microsof...