Basic pattern for keeping a collection of view models in sync with the model

Aug 22, 2011 at 7:39 PM

I'm trying to evaluate Obtics to keep a collection of view-models in sync with the underlying model collection as follows.  The basic query would look something like this:

ObticsItemList =  Model.ItemList.Select(item => new ItemViewModel(item));

In addition to keeping the set of items in the two lists in sync, it also needs to preserve the order, particularly when an  item in Model.ItemList is moved from one position in the list to another (using RemoveAt() and Insert()).

Are there any requirements on on the model and view-model items (e.g. Equals(), HashCode()) in order for this to work?

I'm currently using Continuous Linq, but it doesn't seem to support the last requirement for moving an item in the source list.

Could you provide a code example?




Aug 23, 2011 at 4:33 PM

The only thing that is important is that your lambda expression is 'well behaved'.

The result of the lambda should always be the same if the input is the same. Wether two things are the same is determined by the default equality comparer. (EqualityComparer<T>.Default)




Sep 13, 2011 at 1:48 AM
Edited Sep 13, 2011 at 2:58 PM

I'm getting unexpected behavior.  My query is:

Children = ExpressionObserver.Execute(this, t => Composite.Members.OfType<Attribute>().Select(member => new AttributeNode(member, this))).Cascade();

When I add a new Attribute instance (e.g. attribute1) to Composite.Members, then 2 AttributeNodes containing attribute1 appear in Children.

When I add a second instance (e.g. attribute2), the Children collection now (corrrectly) has a single instance of attribute1, but 2 instances of attribute2.  Etc.

However this problem only occurs when the Children collection is bound to a view, otherwise it works as expected.


What could I be missing?  Thanks.

Sep 15, 2011 at 11:23 PM

Well, that shouldn't happen. Could you post some code that demonstrates this behaviour? Then we can see if something is wrong with the code or if there is a bug in Obtics.