Imagine you have Listener class that will connect on app start to some server with Connect method, then it starts to listen to messages and on every received message it raises event OnRecievedMessage. Based on these messages you need to update viewmodels which in turn update views. What’s more you have to run Listen() method on new thread in order to don’t block UI, so event OnRecievedMessage will also run on this new thread.
How would you incorporate such class in structure of WPF/MVVM application and how to connect it with viewmodel?
In this scenario I would not use an event – I would expose
IObservable<T>Because the IObservable will be an object and can be easily passed and returned to/from a function you gain:
So don’t pass your “Listener” class around – pass the IObservable you implement/expose there around!
Make sure to have a look at the Rx-Framework