views:

65

answers:

1

A common approach for communication between 2 ViewModels is this: http://stackoverflow.com/questions/2474768/mvvm-view-model-view-model-communications

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

NewSchoolclassUserControl NewPupilUserControl

SchoolclassListUserControl PupilListUserControl

PupilsDetailUserControl 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!

A: 

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.

blindmeis
well my viewmodels refer to other viewmodels, because if you select a schoolclass in the schoolclassListViewModel the list of pupils in the PupilListViewModel must change. Aggregating ViewModels is a no go I have heard...
Lisa
if you have references from viewmodel to viewmodel you did not need a mediator, you can use events eg. maybe you can post some code(vm + view). what mvvm approach do you go? view first or viewmodel first?
blindmeis
I do atm View first approach but probably I will switch to ViewModel first because of MEFedMVVM and services etc... back to topic:
Lisa
back to topic. it does not matter how many usercontrols do you have in one window. to real question is how are your viewmodels are related/referenced to each other and how should the viewmodels communicate. mediator/ messenger pattern is just one opportunity to achieve loosly coupled design between your viewmodel. maybe you should specify your question a little bit?
blindmeis
sorry the back to topic was stupid... I sent the post but have actually not finished it but I could not edit it anymore... now I have updated my init post! :) thanks for reading dude.
Lisa
@blindmeis yes it does not fit in my design, but sounds interesting :) What do you say about the code below my this.RaiseProp.... ? Do you think that is ok? I use Action delegates instead of ICommands for subscribing.
Lisa
@blindmeise "...maybe you should check some sample projects from over the www :)..." I studied many mvvm samples: WAF,Chinch,J.Smith msdn article,MefedMVVM,Ocean,Stuff, etc... all do it differently and all offer samples with really simple GUI nothing complex like Customer-Order-Products aggregates.
Lisa
@lisa, sure you did, i just wanted to mention that all do mvvm differently :) and to the initial question "Communication between 6 ViewModels and a Messenger == AntiPattern ?" no its not :) if you want to know wether your code can be improved just open a new thread with a related question. happy sunday ps: if you find a post helpful mark it as useful:)
blindmeis
yes happy sunday to you too :) I think I can mark your answer as solution because its sunday :P
Lisa