Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>All the creation-related issues can be resolved with introduction of <a href="http://en.wikipedia.org/wiki/Factory_method_pattern" rel="nofollow">factory design pattern</a>. The factory will take care of creating <strong>view models</strong> basing on model that was provided. </p> <pre><code>public class MainWindowViewModel { private List&lt;Model&gt; _NamesModel; private ObservableCollection&lt;ViewModel&gt; _NamesViewModel; private IViewModelFactory factory; public void MainWindowViewModel(IViewModelFactory factory) { //Lets pretend we have a service that returns a list of models _NamesModel = Service.Request(); _NamesViewModel = factory.CreateNamesViewModels(_NamesModel); } public ObservableCollection&lt;ViewModel&gt; NamesViewModel { get { return _NamesViewModel; } } } </code></pre> <p>What is more, you could even get rid of <code>Service</code> dependency in view model and move it to the factory itself, thus reducing the need to keep model in view model (admittedly though, removal of model might not work in more complex scenarios):</p> <pre><code>public ObservableCollection&lt;ViewModel&gt; CreateNamesViewModels() { var models = Service.Request(); return new ObservableCollection(models.Select(m =&gt; new ViewModel(m))); } </code></pre> <p>Also, your main window view model can expose <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx" rel="nofollow"><code>commands</code></a> that utilize factory to create any new instances. This way, no model is leaking to view and also no creation details are exposed (since commands will hide actual implementation).</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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