title: Immutable data model for an WPF application with MVVM implementation
body: <p>I have an application which has a data tree as a databackend. To implement the the MVVM pattern I have a logic layer of classes which encapsulate the data tree. Therefore the logic also is arranged in a tree. If the input is in a valid state the data should be copied to a second thread which works as a double buffer of the last valid state. Therefore one way would be cloning. </p>
  
  <p>Another approach would be to implement the complete data backend to be immutable. This would imply to rebuild the whole data tree if something new is entered. My question is, is there a practical way to do this? I'm stuck at the point where I have to reassign the data tree efficently to the logic layer.</p>
  
  <p>**UPDATE - Some Code</p>
  
  <p>What we are doing is to abstract hardware devices which we use to run our experiments. Therefore we defined classes like "chassis, sequence, card, channel, step". Those build a tree structure like this:</p>
  
  <pre><code>                            Chassis
                            /      \
                     Sequence1      Sequence2
                     /    |    \
                Card1   Card2  Card3
               /     \
       Channel1      Channel2
      /        \
  Step1        Step2
  </code></pre>
  
  <p>In code it looks like this:</p>
  
  <pre><code>public class Chassis{
  
  readonly var List&lt;Sequence&gt; Sequences = new List&lt;Sequence&gt;();
  
  }
  
  public class Sequence{
  
  readonly var List&lt;Card&gt; Cards = new List&lt;Card&gt;();
  
  }
  </code></pre>
  
  <p>and so on. Of course each class has some more properties but those are easy to handle. My Problem now is that List is a mutable object. I can call List.Add() and it changed. Ok there is a ReadOnlyList but I'm not sure if it implements the immutability the right way. Right as in copy by value not reference and not just blocking to write to it by blocking the set methods. 
  The next problem is that the amount of sequences and step can vary. For this reason I need an atomic exchange of list elements. 
  At the moment I don't have any more code as I'm still thinking if this way would help me and if it is possible at all to implement it in a reasonable amount of time.</p>