Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM + Reactive + WCF callback

I am using MVVM + Reactive Extensions against a TCP.NET WCF service that has callbacks. So I created a ClientProxy class the receives the callbacks. I have the ClientProxy that has a Subject and I Subcribe to it in the Model. Now what I would like to do it push the item received to the ViewModel without having to reference the ViewModel. Note(I have changed the actually item names so if I have something mispelled, it is not really how my code is).

Model:

public Subject<CustomSale> CustomSaleAdded = new Subject<CustomSale>();

ClientProxy._onCustomSaleAdded.Subscribe(x =>
{
    CustomSaleAdded.OnNext(x);
});

And then in my ViewModel, I am just trying to Subscribe to the Model Subject CustomSaleAdded and add to my ObservableCollection<Sale> Sales:

_scheduleModel.CustomSaleAdded.Subscribe(x =>
{
    Sales.Add(x);
});

What is happening is that the item gets from the ClientProxy to the Model and I see it fires CustomSaleAdded.OnNext(x). But in the ViewModel, I have set a break point and nothing happens.

So could someone tell me what would be the best way to push an item from a WCF callback up to a ObservableCollection in a ViewModel without the ViewModel having to reference the Model using Reactive Extensions?

Also, is this something Reactive UI handles?

Most samples I see show a ViewModel working directly with a Web Service. I have two additional layers in the Model and then the ClientProxy that first get the item and push it.

like image 217
Julie Wells Avatar asked Mar 31 '26 12:03

Julie Wells


1 Answers

It seems the Subject<T> class should not be used to implement the CustomSaleAdded member since the Subject<T> has the following behavior (Subject Class, Remarks section):

The data is then published through it's IObservable interface to all subscribed observers.

The root cause: the ViewModel class can "miss" some elements of the observable sequence in case when the ViewModel subscribes after some elements are pushed through the sequence.

Please consider using another implementation of ISubject<T> interface:

  • ReplaySubject Class:

    A ReplaySubject buffers items it receives. So a subscription created at a later time can access items from the buffered sequence even if they have already been published. How many items a ReplaySubject will buffer depends on the parameters passed to the constructor.

  • BehaviorSubject Class:

    A BehaviorSubject buffers the last item it published through its IObservable interface. If no item has been published through its IObservable interface then the initial item provided in the constructor is the currently buffered item. When a subscription is made to the BehaviorSubject's IObservable interface, the sequence published begins with the currently buffered item.

    No items are buffered or published from a BehaviorSubject once its IObserver interface receives a completion.

Additional references:

  • Using Subjects, MSDN.
  • Key types, Intro To Rx, Lee Campbell.
like image 128
Sergey Vyacheslavovich Brunov Avatar answered Apr 03 '26 01:04

Sergey Vyacheslavovich Brunov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!