none
Net Core: Even Number Detector with Reactive System RRS feed

  • Question

  • I am trying to setup a simple Reactive Extension example. Eg: there is Random  Number Generator, and anytime Even Number divisible by 2 is spotted, "Write Hello Even Number" and Observe the Event. It does not seem to be working. Feel free to edit line, or rewrite code to make work correctly. Its a quick two page copy into  Console Program. 

    Eventually, want number generator to work in a stream , while SeeNumber observes in a Console program for one minute. 


    Code follows this example: https://rehansaeed.com/reactive-extensions-part1-replacing-events/

    Goals which which can be incorporated in code:

     1. Make the Number Generator streaming
     2. Also reading, are Subjects are good in RX? Feel free to modify code without subjects if true,

    Main.cs
        static void Main(string[] args)
        {
            var generateNumber = new GenerateNumber();
            var seeNumber = new SeeNumber(generateNumber);
            generateNumber.DivideByTwoSpotted.Subscribe(seeNumber.OnDivideByTwoSpotted);
    
            Console.WriteLine(generateNumber.NumberValue);
            Console.ReadKey();
        }



    Produce Number:
        public class GenerateNumber
        {
            public int NumberValue { get; set; }
    
            public GenerateNumber()
            {
                NumberValue = CreateData();
            }
    
            public Subject<GenerateNumber> divideByTwoSpotted = new Subject<GenerateNumber>();
    
            public IObservable<GenerateNumber> DivideByTwoSpotted
            {
                get { return this.divideByTwoSpotted; }
            }
    
            public int CreateData()
            {
                Random random = new Random();
                return random.Next(0, 100);
            }
    
            public void SpotDivideByTwo(GenerateNumber generateNumber)
            {
                try
                {
                    if (NumberValue % 2 == 0)
                    {
                        Console.WriteLine("Number is Even");
                        this.divideByTwoSpotted.OnNext(generateNumber);
                    }
                }
                catch (Exception exception)
                {
                    this.divideByTwoSpotted.OnError(exception);
                }
            }
        }


    See Number:
        public class SeeNumber : IDisposable
        {
            private IDisposable divideByTwoSpottedSubscription;
    
            public SeeNumber(GenerateNumber generateNumber)
            {
                this.divideByTwoSpottedSubscription = generateNumber.DivideByTwoSpotted.Subscribe(this.OnDivideByTwoSpotted);
            }
    
            public void Dispose()
            {
                this.divideByTwoSpottedSubscription.Dispose();
            }
    
            public void OnDivideByTwoSpotted(GenerateNumber generateNumber)
            {
                GenerateNumber spottedDivisibleByTwo = generateNumber;
            }
        }


    Creating a simple example from tutorial,

      [1]: https://rehansaeed.com/reactive-extensions-part1-replacing-events/


    Tuesday, October 8, 2019 7:27 PM