Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between two viewmodels

I'm newbie in MVVM design pattern, and I have these viewmodels :

ClassAViewModel

public class ClassAViewModel : INotifyPropertyChanged
    {
        private int _nbre = 0;

        public int Nbre
        {
            get
            {
                return _nbre;
            }
            set
            {
                _nbre = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Nbre"));
            }
        }

        #region Events
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

    }

And ClassBViewModel

 PUBLIC class ClassBViewModel: INotifyPropertyChanged
    {
        private Boolean _IsBiggerthanFive = false;

        public bool IsBiggerthanFive
        {
            get
            {
                return _IsBiggerthanFive;
            }
            set
            {
                _IsBiggerthanFive = value;
                PropertyChanged(this, new PropertyChangedEventArgs("IsBiggerthanFive"));
            }
        }

        #region Events
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

    }

I need to know if a mecanism of notification between two viewmodels exists , ie in my case if _nbre > 5 in the first viewmodel, the second viewmodel will be notified and the value of _IsBiggerthanFive will be changed. So:

  1. How can two viewmodels communicate between them without instanciate one in the other ?
  2. What is the best way to accomplish this task?
like image 666
Lamloumi Afif Avatar asked Sep 30 '22 05:09

Lamloumi Afif


1 Answers

I agree with other commenters that the mediator/pub-sub/event aggregator/messenger is a good way to go. If you're not using an MVVM framework with a built-in solution, then I recommend this simple approach that takes advantage of the Reactive extensions:

public class EventPublisher : IEventPublisher
{
    private readonly ConcurrentDictionary<Type, object> subjects
        = new ConcurrentDictionary<Type, object>();

    public IObservable<TEvent> GetEvent<TEvent>()
    {
        var subject =
            (ISubject<TEvent>) subjects.GetOrAdd(typeof (TEvent),
                        t => new Subject<TEvent>());
        return subject.AsObservable();
    }

    public void Publish<TEvent>(TEvent sampleEvent)
    {
        object subject;
        if (subjects.TryGetValue(typeof(TEvent), out subject))
        {
            ((ISubject<TEvent>)subject)
                .OnNext(sampleEvent);
        }
    }
}

That's your whole event aggregator. Pass an instance of it into each view model, and store it as a reference. Then create a class to store your event details, let's say "ValueChangedEvent":

public class ValueChangedEvent
{
    public int Value
    {
        get { return _value; }
    }
    private readonly int _value;

    public ValueChangedEvent(int value)
    {
        _value = value;
    }
}

Publish like this from the first view model:

set
{
    _nbre = value;
    PropertyChanged(this, new PropertyChangedEventArgs("Nbre"));
    _eventPublisher.Publish(new ValueChangedEvent(value));
}

Subscribe in the other class using GetEvent:

public class ClassBViewModel: INotifyPropertyChanged, IDisposable
{
    private readonly IDisposable _subscriber;

    public ClassBViewModel(IEventPublisher eventPublisher)
    {
        _subscriber = eventPublisher.Subscribe<ValueChangedEvent>(next => 
        {
            IsBiggerthanFive = next.Value > 5;
        });
    }

    public void Dispose()
    {
        _subscriber.Dispose();
    }
}
like image 74
McGarnagle Avatar answered Oct 03 '22 06:10

McGarnagle