Communication between 6 ViewModels and a Messenger == AntiPattern?

最后都变了- 提交于 2020-01-05 08:54:51

问题


A common approach for communication between 2 ViewModels is this: MVVM- View Model-View Model Communications

The Mediator pattern or a Messenger class. But what about 6 ViewModels in one Window?

  1. NewSchoolclassUserControl
  2. NewPupilUserControl
  3. SchoolclassListUserControl
  4. PupilListUserControl
  5. PupilsDetailUserControl
  6. AdministrationButtonBarUserControl (having buttons executing commands)

All this is in ONE Window. Do "you" really tell me now I have to setup a Messenger for those 6 Views and their Viewodels? That would be terrible...

6 UserControls in one Window, not even a big enterprise app has more UserControls in a window, so whats an accepted/best practice in that case?

I would be interested in someones opinion having experience with big mvvm applications :)

Some of those UserControl+ViewModels I would like to reuse at other places of my application. So put all in one UserControl is not that what I really want.

UPDATE: for the blind meise ;-)

private DateTime _selectedDate;
        public DateTime SelectedDate
        {
            get { return _selectedDate; }
            set
            {
                if (_selectedDate == value)
                    return;

                _selectedDate = value;
                this.RaisePropertyChanged("SelectedDate");


                ObservableCollection<Period> periods = _lessonplannerRepo.GetLessonDayByDate(SelectedDate);

                _periodListViewModel = new ObservableCollection<PeriodViewModel>();

                foreach (Period period in periods)
                {
                    PeriodViewModel periodViewModel = new PeriodViewModel(period);

                    foreach (DocumentListViewModel documentListViewModel in periodViewModel.DocumentViewModelList)
                    {
                        documentListViewModel.DeleteDocumentDelegate += new Action<List<Document>>(OnDeleteDocument);
                        documentListViewModel.AddDocumentDelegate += new Action(OnAddDocument);
                        documentListViewModel.OpenDocumentDelegate += new Action<Document>(OnOpenDocument);
                    }

                    _periodListViewModel.Add(periodViewModel);                    

                } 
            }
        }

@blindmeise

This ViewModel is datatemplated actually to a DataGrid. The Periods are the Rows. Each Row has a Column called Documents. I have a PeriodListViewModel 1 : N DocumentListViewModel.

The DocumentListViewModel is datatemplated with a UserControl containing a ListBox and below some buttons add/del/save/open etc...

A DocumentListViewModel has Commands and Action delegates executed in the "LessonController" so every action on a Document like add,del etc... can be done on the SelectedPeriodViewModel declared in the LessonController.

The above code just loads new data from database when the user changes the date in the datepicker.

Do you need more code or what do you say about my approach? I am eager to learn and I am glad about every critics!


回答1:


if you have 6 or 1000 loosly coupled viewmodels which should communicate with each other, then you should use the messenger/mediator. it has nothing to do with usercontrols at all.

if your viewmodels reference to each other then you have no need for a messenger, but its no more loosly coupled then :)

edit: its really hard to say what can you to better, cause i dont know what do you want to achieve with your app and your appdesign :) in general it depends on how you specify the tasks for your viewmodels and how do you want to couple these viewmodels. maybe you should check some sample projects from over the www :) there are a lot mvvm implementation which vary a lot but gives a better understanding on the mvvm pattern(pattern!! not rule!! ;))

imagin you have a viewmodel which do nothing then select a date. this would be a simple loosly coupled vm. all you can do now is when a new date is selected, send a message through a messenger.

public DateTime SelectedDate
{
    get { return _selectedDate; }
    set
    {
         if (_selectedDate == value)
             return;

          _selectedDate = value;
          this.RaisePropertyChanged("SelectedDate");

          this.messenger.Notify("SelectedDateChanged", this.SelectedDate)
     }
 }

now all other loosly coupled viewmodels could register to the mediator and the "SelectedDateChanged" message and do what the want to do when a date changed. this maybe does not fit in your design, but should give you an idea on the messenger pattern.



来源:https://stackoverflow.com/questions/3928179/communication-between-6-viewmodels-and-a-messenger-antipattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!