Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere to store application settings/state in a MVVM application
    text
    copied!<p>I'm experimenting with MVVM for the first time and really like the separation of responsibilities. Of course any design pattern only solves many problems - not all. So I'm trying to figure out where to store application state and where to store application wide commands. </p> <p>Lets say my application connects to a specific URL. I have a ConnectionWindow and a ConnectionViewModel that support gathering this information from the user and invoking commands to connect to the address. The next time the application starts, I want to reconnect to this same address without prompting the user. </p> <p>My solution so far is to create an ApplicationViewModel that provides a command to connect to a specific address and to save that address to some persistent storage (where it's actually saved is irrelevant for this question). Below is an abbreviated class model.</p> <p>The application view model:</p> <pre><code>public class ApplicationViewModel : INotifyPropertyChanged { public Uri Address{ get; set; } public void ConnectTo( Uri address ) { // Connect to the address // Save the addres in persistent storage for later re-use Address = address; } ... } </code></pre> <p>The connection view model:</p> <pre><code>public class ConnectionViewModel : INotifyPropertyChanged { private ApplicationViewModel _appModel; public ConnectionViewModel( ApplicationViewModel model ) { _appModel = model; } public ICommand ConnectCmd { get { if( _connectCmd == null ) { _connectCmd = new LambdaCommand( p =&gt; _appModel.ConnectTo( Address ), p =&gt; Address != null ); } return _connectCmd; } } public Uri Address{ get; set; } ... } </code></pre> <p>So the question is this: Is an ApplicationViewModel the right way to handle this? How else might you store application state?</p> <p><strong>EDIT:</strong> I'd like to know also how this affects testability. One of the primary reasons for using MVVM is the ability to test the models without a host application. Specifically I'm interested in insight on how centralized app settings affect testability and the ability to mock out the dependent models.</p>
 

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