3

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.

Stephan Jennewein
  • 303
  • 1
  • 2
  • 10
  • 2
    It's very counter-intuitive for an MVVM app to be backed by an immutable model. I'd look into another pattern. – Louis Kottmann Nov 07 '12 at 12:03
  • I am having the same situation mapping a complex immutable model to MVVM, it does not seem straightforward. How did you solve this in the end? – Oskar Sjöberg Jan 12 '16 at 06:47

2 Answers2

4

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.

Brandon Bonds
  • 203
  • 1
  • 9
0

I'm not sure if I understand 100% what you're asking. It sounds like you have a tree of objects in a particular state and you want to perform some processing on a copy of that without modifying the original object state. You should look into cloning via a "Deep Copy". This question should get you started.

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

public class Chassis
{

   List<Sequence> _sequences = new List<Sequence>();
   public IEnumerable<Sequence> Sequences { get { return _sequences; } }

}

18/04/13 Update in response to Brandon Bonds comments

The library linked in Brandon Bonds answer is certainly interesting and offers advantages over IEnumerable<>. In many cases it is probably a better solution. However, there are a couple of caveats that you should be aware of if you use this library.

  1. As of 18/04/2013 This is a beta library. It is obviously still in development and may not be ready for production use. For example, The code sample for list creation in the linked article doesn't work in the current nuget package.

  2. This is a .net 4.5 library. so it will not be suitable for programs targeting an older framework.

  3. It does not guarantee immutability of objects contained in the collections only of the collection itself. It is possible to modify objects in an immutable list You will still need to consider a deep copy for copying collections.

This is addressed in the FAQ at the end of the article

Q: Can I only store immutable data in these immutable collections?

A: You can store all types of data in these collections. The only immutable aspect is the collections themselves, not the items they contain.

In addition the following code sample illustrates this point (using version 1.0.8-beta)

class Data
{
    public int Value { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var test = ImmutableList.Create<Data>();           
        test = test.Add(new Data  { Value = 1 });
        Console.WriteLine(test[0].Value);
        test[0].Value = 2;
        Console.WriteLine(test[0].Value);
        Console.ReadKey();
    }
}

This code will allow modification of the Data object and output

1
2

Here are a couple of articles for further reading on this topic

Community
  • 1
  • 1
David Turvey
  • 2,891
  • 6
  • 27
  • 29