Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does one manage object lifetime in MVVM?
    primarykey
    data
    text
    <p>I'm working on an application built using C# and WPF, which (badly) implements MVVM. The breakdown of work is something like:</p> <ul> <li><p>View</p> <ul> <li>Presentation, bitmaps, colors, etc.</li> <li>Animations (if used)</li> <li>Communicates with the ViewModel by being data bound to it</li> <li>Communicates with the ViewModel by raising commands on it</li> </ul></li> <li><p>ViewModel</p> <ul> <li>Exposes commands to be invoked by view(s)</li> <li>Delegates data processing functionality to the model</li> <li>Defines UI behavior</li> <li>No direct (named) dependency on the view</li> <li>Communicates with the Model by calling methods in the model</li> <li>May be notified of changes in the model by subscribing to events exposed by the model</li> </ul></li> <li><p>Model</p> <ul> <li>Disk persistence, data analysis, etc</li> <li>Everything else</li> <li>No dependency on the ViewModel</li> </ul></li> </ul> <p>Unfortunately, this has resulted in circular references, because view(s) need references to viewmodels to raise events and fire commands, viewmodels need references to views in order to update view state (typically this reference is by a viewmodel being WPF's <code>DataContext</code>), viewmodels need a reference to models to delegate work, and models often need to notify viewmodels of some external change in state.</p> <p>So we have two circular reference problems, with the viewmodel sitting in the middle. As a result, this application is running into memory consumption problems because WPF entities are being created and associated with some piece of data in the model, and these entities are never cleaned up (until the program is terminated).</p> <p>How is this supposed to be handled?</p> <p>It seems like an ownership graph needs to be defined, such that one or more of these components is responsible for disconnecting event handlers when they are no longer relevant, so that things can be GC'd.</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