Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can a ViewModel know when data in a service is updated?
    primarykey
    data
    text
    <p>In my application, I've got several ViewModels that have a single service (repository, DAO, whatever), let's call it the <strong>WidgetService</strong>, injected into them. </p> <p>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.</p> <p>The user can view the list of widgets in the <strong>WidgetListView</strong> backed by a <strong>WidgetListViewModel</strong> and click a button to add a new widget. To create this new Widget, a <strong>CreateWidgetViewModel</strong> is new'd up and injected into the DataContext of some UserControl/Window thus, through the magic of DataTemplates displaying the <strong>CreateWidgetViewModel</strong> in a <strong>CreateWidgetView</strong>. Also, the newing up of the <strong>CreateWidgetViewModel</strong> does not necessarily happen within the scope of the <strong>WidgetListViewModel</strong>.</p> <p>When the <strong>WidgetListViewModel</strong> it was injected with an instance of the <strong>WidgetService</strong> . The <strong>CreateWidgetViewModel</strong> was injected with this same <strong>WidgetService</strong> instance.</p> <p>Now, when the user clicks <em>save</em> in the <strong>CreateWidgetView</strong> the <em>Save</em> method on the <strong>WidgetService</strong> will be invoked and the widget will be persisted. Now the <strong>WidgetListViewModel</strong> needs to be notified that there is a new Widget to be displayed!</p> <p>The long buildup leads to this question: How do I let the <strong>WidgetListViewModel</strong> know that it needs to display the new Widget?</p> <p>I've seen a <a href="http://www.lab49.com/files/videos/Jason%20Dolinger%20MVVM.wmv" rel="nofollow noreferrer">Video</a> 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?</p> <p>Does anyone have any suggestions regarding this?</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload