Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immutable data model for an WPF application with MVVM implementation

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.

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.

**UPDATE - Some Code

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:

                            Chassis
                          /      \
                   Sequence1      Sequence2
                   /    |    \
              Card1   Card2  Card3
             /     \
     Channel1      Channel2
    /        \
Step1        Step2

In code it looks like this:

public class Chassis{

readonly var List<Sequence> Sequences = new List<Sequence>();

}

public class Sequence{

readonly var List<Card> Cards = new List<Card>();

}

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.

like image 1000
Stephan Jennewein Avatar asked Oct 20 '25 13:10

Stephan Jennewein


1 Answers

Note that there are new immutable collections for .NET that could help you achieve your goal.

Be very cautious about Dave Turvey's statement (I would downvote/comment if I could):

If you are looking to implement an immutable list you could try storing the list as a private member but exposing a public IEnumerable<>

This is incorrect. The private member could still be changed by its container class. The public member could be cast to List<T>, IList<T>, or ICollection<T> without throwing an exception. Thus anything that depends on the immutability of the public IEnumerable<T> could break.

like image 198
Brandon Bonds Avatar answered Oct 22 '25 04:10

Brandon Bonds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!