1

is there any way to handle events from mediator inside the ViewModel. If VM implemetns interface INotification it must be singleton or creates/destroy based on mediators rules.

Or how to solve this issue. For example: when i create new Customer(), domain model generates event CustomerCreatedEvent() and i want to update counter on my mobile app UI.

I know there are events in c#, where can i subscribe/unsubscribe VM but it is "diffrend kind" of event.

Thanks for advice.

Update 01 I found a solution when VM and Notification are independet units.

public class CustomerCreatedHandler : INotification<CustomerCreatedEvent>
    {
        private readonly CustomerPageViewModel _viewModel;


        public CustomerCreatedHandler(CustomerPageViewModel viewModel)
        {
            _viewModel = viewModel;
        }

        public Task Handle(CustomerCreatedEvent customerCreatedEvent)
        {
            // set property of VM
            _viewModel.Count = customerCreatedEvent.Count;

            return Task.CompletedTask;
        }
    }

It should be working. VM is singleton and Notification handler can be destroyed after work is finished.

  • Does [INotifyPropertyChanged](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.inotifypropertychanged?view=net-6.0) help ? `PropertyChanged` raises whenever the property value gets changed . – ColeX Feb 17 '22 at 09:07
  • You are right. PropertyChanged is second step. But i need set some property from external source. I updated first post with idea. – Lukas KUCHTA Feb 17 '22 at 11:53

0 Answers0