0

How do I create an Observable for a class? Eg here is class for continuous sales

public class Product
{
     public int ProductId {get;set;}
     public string ProductDescription {get;set;}
     public float Sales{get;set;}
}

How to create Observable? Say they are coming into a List<Product> with Add(), products keep getting added representing transactions.

Anytime, a product is added anywhere in Console program, the subscriber will observe it.

var productSample1 = new Product { ProductId = 1, ProductDescription = "Furniture" };
var productSample2 = new Product { ProductId = 2, ProductDescription = "Book" };
var productSample3 = new Product { ProductId = 3, ProductDescription = "car" };

var productlist = new List<Product>();
productlist.Add(productSample1);
....
productlist.Add(productSample2);
...
productlist.Add(productSample3);

Maybe something like this,

var products = Observable.FromEventPattern < EventHandler<Product>, Product>();

Trying to read resources below, trying to add on ,

Generate numbers at random time intervals with rx

Intro to Rx

  • what is the lifetime of your generator (how Products are incoming)? It's not that easy to answer. It could be a cold/hot observable, Observable.FromEventPattern/ Observable.Create/ ... – ntohl Oct 09 '19 at 07:04
  • hi @ntohl say 20 products a minute, it can be a hot observable –  Oct 09 '19 at 07:13
  • How does your generation look like? Where is the code, where the `new Product` would be? Or that is what you like to delegate to Observable? – ntohl Oct 09 '19 at 10:26

1 Answers1

0

You can either use an ObservableCollection:

var list = new ObservableCollection<int>();
using (var o = Observable
    .FromEventPattern<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(h => list.CollectionChanged += h, h => list.CollectionChanged -= h)
    .Subscribe(e => Console.WriteLine($"{e.EventArgs.Action} {e.EventArgs.NewItems[0]}")))
{
    list.Add(1);
    list.Add(10);
}

// Add 1
// Add 10

Or turn it around and add from a subscription:

var list = new List<int>();
var subject = new Subject<int>();
using (var o = subject.Subscribe(i => list.Add(i)))
{
    subject.OnNext(1);
    subject.OnNext(10);
}

foreach (var i in list)
{
    Console.WriteLine(i);
}

// 1
// 10
Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59
  • Thanks, and anytime I run the statement, list.Add(productSample3), or, list.Add(productSample4), or list.Add(productSample5), in another totally separate statements, it will, automatically read into Observable? –  Oct 09 '19 at 15:06
  • also, another question here, https://stackoverflow.com/questions/58296396/entity-framework-convert-changetracker-to-observable-with-reactive-stream?noredirect=1&lq=1 –  Oct 09 '19 at 15:08
  • @earlwatkins85, what do you mean by "automatically read into Observable"? – Paulo Morgado Oct 09 '19 at 15:57
  • I described more in question, also; why did we get rid of Product and replace with int, trying to specify answer to question for custom class, thanks –  Oct 09 '19 at 16:16
  • The item type doesn't matter. This answer is valid for any item type and has the corresponding console output in the comments. – Paulo Morgado Oct 09 '19 at 18:15