Hey folks,
I got an MVVM dotnet project using the mvvm community toolkit.
Now I am facing a situation where I have one viewmodel A which offers a user action that will modify the database and because
of this change my viewmodel B needs requery the database and update its data.
I came across events or messaging which provides me with an option for viewmodel-to-viewmodel communication, but afaik
all options only support sync function execution and no async option (but I deal with database so it must be async I think).
My current way to solve it is the following, but I am unsure if this is the recommended common best approach.
CommunityToolkit.Mvvm.Messaging.Messages;
public sealed class NotifyMessage : ValueChangedMessage
{
public NotifyMessage(string value) : base(value) { }
} using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
public partial class ViewModelA : ObservableObject
{
public ViewModelA()
{
}
private void UpdateData()
{
// do stuff
SendMessage();
}
private void SendMessage()
{
WeakReferenceMessenger.Default.Send(new NotifyMessage("Data update in A"));
}
}
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using System.Diagnostics;
public partial class ViewModelB : ObservableObject, IRecipient, IDisposable
{
[ObservableProperty] private string _lastMessage = "B";
public ViewModelB()
{
WeakReferenceMessenger.Default.Register(this);
}
public async void Receive(NotifyMessage message)
{
LastMessage = message.Value;
try {
await RefreshData();
}
catch (Exception e)
{
Console.WriteLine("ERROR: " + e.Message);
}
}
private async Task RefreshData()
{
await Task.Delay(1000);
Console.WriteLine(LastMessage);
}
public void Dispose()
{
WeakReferenceMessenger.Default.Unregister(this);
}
}
In viewmodel B I have one "async void" which is bad. But I don't see how to get around that otherwise. Or do I have a architectural issue and this should not rise?
I am a bit confused because this should be a quite common problem, but I didn't found any way to solve it in a good way.
Sam HobbsPosted Dec 2, 2025, 6:31 PM
You say multiple Viewmodels need to update their data after such an action and ObservableCollection will notify its users when data is added or deleted. When a data item is updated the INotifyPropertyChanged interface provides notification. Perhaps you are avoiding ObservableCollection because it seems to be too much work but it will likely be less work overall to use it.
Guest UserPosted Nov 30, 2025, 6:42 PM
Hey Sam,
thanks for reply. Your mentioned messaging system is already in the example. The Viewmodels don't communicate directly.
True I didn't use a ObservableCollection in this example. But the point is the Viewmodels don't share any resources on this abstraction level. It is on the data level. So one action changes multiple things in the database and as different Viewmodels are based on that database -> multiple Viewmodels need to update their data after such an action.
Sam HobbsPosted Nov 23, 2025, 8:55 PM
I am not experienced with MVVM but I have some suggestions, with the help of AI. First, ViewModels should not communicate directly. They should communicate using an event or messaging system (like an event aggregator, messenger, or mediator pattern). I suspected that and AI says so.
You say requery the database and update its data and you use ObservableObject but I do not see you using ObservableCollection. If your ViewModels share ObservableCollection items then they will be notified by the .Net classes.