开发者

Communication between 6 ViewModels and a Messenger == AntiPattern?

开发者 https://www.devze.com 2023-01-20 08:32 出处:网络
A common approach for communication between 2 ViewModels is this: MVVM- View Model-View Model Communications

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...开发者_JS百科

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消