0

I have an intermediate view model that coordinates the communication between two view model of the same type.

This is the code:

public class DocumentViewModel : ObservableObject
{

    [PbservbleProperty]
    ObservableCollection<DOcument> _documents = new ObservableCollection();


    [ObservableProperty]
    Document? _selectedDocument;

    partial void OnSelectedDocumentChanged(Document? value)
    {
        WeakeReference.Default.Send<DocumentSelectedMessage>(value);
    }


    public RefreshDocuemnts(IEnumerable<Documents> paramDocuments)
    {
        Documents.Clear();
        Documents.AddRange(paramDocuments);            
    }
}




public class IntermediateViewModel : ObservableObject
{
    [ObservableProperty]
    DocumentViewModel _mainDocumentViewModel;

    [ObservableProperty]
    DocumentViewModel _relatedDocumentsViewModel;


    public IntermediateViewModel(DocumentViewModel paramMainDocumentViewModel, DocumentViewModel paramRelatedDocumentsViewModel)
    {
        _mainDocumentViewModel = paramMainDocumentViewModel;
        _relatedDocumentsViewModel = paramRelatedDocumentsViewModel;

        
    }



    private void RegisterMessengers()
    {
        WeakReferenceMessenger.Default.Register<DocumentSelectedMessage>(this, (r, m) =>
        {
            OnSelectedDocument();
        });
    }

    private void OnSelectedDocument(Document paramSelectedDocument)
    {
        _relatedDocumentsViewModel.RefreshDocuemnts(paramSelectedDocument.RelatedDocuments);
    }
}

Basically what it has it the intermediate view model subscribe to the messenger to know if a document is selected.

I want that when a document is seleccted in the main view model, to update the related documents in the related documents view model.

The problem with this solution is that the intermediate view model receive the message when a document is selected in the main view model or in the related documents view model, so if it receives the message from the related documents view model, it will refresh the related documents view model with the wrong documents.

So I would like to know if there is some way to check if the message is from the main view model or from the related documents view model.

I have see the parameters this, r and m doesn´t tells who sends the message, just tells who is the received, in this case, ItermediateViewModel, so I can't do nothing in the case the message is from the related documents view model.

Or perhaps I am doing it in the wrong way, perhaps I should to subscribe to the messages directly in the DocumentViewModel instead to pass through the intermediate view model, but I was thinking in this way because I think it is a way to reduce coupling between the document view models, in this way, when the DocumentViewModel is for related documents, doesn't need to know it is for related documents, this information is known by the intermediate view model.

So I would like to know if it is possible to know who is the sender of the message or if there is another options or better solutions, I am open for it.

Thanks.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193

0 Answers0