title: Implementing an own "Factory" for reusing Views in WPF
body: <p>I'm currently using <a href="http://waf.codeplex.com/discussions/361216" rel="nofollow">WAF (WPF Application Framework)</a> for programming in WPF.
  I really like the idea to have an own ViewModel for every small View-Unit in my application which I subsequently implemented in this way.</p>
  
  <p>In my project I got a complex list in which every list-element also contains a list. Each list and list-list element is an own ViewModel because of the complexity. The "worst-case" scenario contains 60-90 viewmodels in total, just for the list-view.</p>
  
  <p>(It's a list of questions where each question has a list of answers with ratings and other ui elements).</p>
  
  <p>This implementation works great but the performance is quite bad. After profiling I found out that the error results in creating my ViewModels when I switch between one set of questions (because the whole list has to be generated again). </p>
  
  <p>When I switch between the question-sets I can't reuse my views 1:1 as there are not the same number of questions.</p>
  
  <p>However, I thought I could reuse the given viewmodels and add (in case the new set requires more views) more viewmodels if necessary.</p>
  
  <p>Therefore I've written the following Factory:</p>
  
  <pre><code>[Export]
  public class ViewModelPerformanceFactory&lt;T&gt; where T : IPerformanceFactoryViewModel
  {
      private List&lt;T&gt; _collection;
      private int _index;
      private readonly ExportFactory&lt;T&gt; _exportFactory;
  
      [ImportingConstructor]
      public ViewModelPerformanceFactory(ExportFactory&lt;T&gt; exportFactory)
      {
          _exportFactory = exportFactory;
          _index = 0;
      }
  
      public void Reset()
      {
          _index = 0;
          if (_collection == null)
          {
              return;
          }
          foreach (var elem in _collection)
          {
              elem.Reset();
          }
      }
  
      public T Get()
      {
          if (_collection == null)
          {
              _collection = new List&lt;T&gt;();
          }
          if (_collection.Count &lt;= _index)
          {
              _collection.Add(_exportFactory.CreateExport().Value);
          }
          return _collection[_index++];
      }
  }
  </code></pre>
  
  <p>where <code>IPerformanceViewModel</code> just offers a <code>Reset</code>-Method to clear the ViewModel and View.</p>
  
  <p>So every time a new question-set is loaded, I call the reset-function of my ViewModelPerformanceFactory which clears all models and set the index back to 0 (so if someone requires a new instance of viewmodel, it will get the first one created).</p>
  
  <p>In theory, this works great. </p>
  
  <p>Now to my question/problem: The more often I switch between my question-sets the slower my application is... It's not the loading of the viewmodel-objects - this is fine. My list just appears very very slow - sometimes even stuck for a few seconds and then continues to build up...</p>
  
  <p>I think this is a WAF-problem as every ViewModel instanciates a View see:</p>
  
  <pre><code>protected ViewModel(TView view) : base(view)
      {
          this.view = view;
      }
  }
  </code></pre>
  
  <p>and it seems like I can't reuse Views as easy as ViewModels in WAF.</p>
  
  <p>Does anyone have a suggestion for me or maybe another approach to speed-up my application? Or does anyone think my whole approach is stupid and I shut stop programming at all? ;)</p>
  
  <p><strong>Edit: There seems to be a memory/performance leak sometimes, but not reproducable every time.. :(</strong></p>