In my application, I've got several ViewModels that have a single service (repository, DAO, whatever), let's call it the WidgetService, injected into them.
Let's say that one of these ViewModels is a listing of all of a users widgets. Another could be the ViewModel for editing/creating a single one of these Widgets.
The user can view the list of widgets in the WidgetListView backed by a WidgetListViewModel and click a button to add a new widget. To create this new Widget, a CreateWidgetViewModel is new'd up and injected into the DataContext of some UserControl/Window thus, through the magic of DataTemplates displaying the CreateWidgetViewModel in a CreateWidgetView. Also, the newing up of the CreateWidgetViewModel does not necessarily happen within the scope of the WidgetListViewModel.
When the WidgetListViewModel it was injected with an instance of the WidgetService . The CreateWidgetViewModel was injected with this same WidgetService instance.
Now, when the user clicks save in the CreateWidgetView the Save method on the WidgetService will be invoked and the widget will be persisted. Now the WidgetListViewModel needs to be notified that there is a new Widget to be displayed!
The long buildup leads to this question: How do I let the WidgetListViewModel know that it needs to display the new Widget?
I've seen a Video in which a guy from Microsoft does this sort of thing using an event on the service that the ViewModel subscribes to. However, the downfall of this is that if the service outlives the viewmodel, then the viewmodel wont get GC'd until the service is GC'd. I could add IDisposable to the ViewModel. But then when/how to call Dispose when the ViewModel is only represented in the UI via DataTemplates?
Does anyone have any suggestions regarding this?
To clarify, I'd say my interpretation of MVVM most closely resembles Josh Smith's. At least in as much as my MVVM architecture pretty closely matches that found in the Crack.Net source.