0

The thing is that sometimes a Model should notify a ViewModel to show a message box, for example. Sometimes a Model should change the state of a ViewModel. What are the proper ways of doing such notifications?

Are there any helpful patterns or something?

EngineerSpock
  • 2,575
  • 4
  • 35
  • 57

1 Answers1

1

There is no single right answer, it is a design decision.

The possible alternatives:

  1. Model can expose event members.
  2. Model can implement IObservable<T> interface or expose the IObservable<T> -members. By the way, there is the Observable.FromEventPattern Method which allows to convert a .NET event to an observable sequence.
  3. Model can use callback interfaces. For example, just to demonstrate the idea:
interface INumberHandler
{
    void Handle(int number);
}

class NumberViewModel : INumberHandler
{
}

class NumberService
{
    public void Calculate(INumberHandler handler)
    {
        handler.Handle(9);
    }
}