locked
Synchronizing DataServiceCollections RRS feed

  • Question

  • Hi.

    Say I have the following scenario:

    I have a global DataServiceContext _dataContext.

    In ViewModel1 I have:

    public DataServiceCollection<Group> GroupCollection = new DataServiceCollection<Groups>(_dataContext.Groups);
    

    In ViewModel2 I have exactly the same property but in some function I write:

    _dataContext.AddToGroups(newGroup);
    this.GroupCollection.Add(newGroup);

    I am finding that ViewModel1.GroupCollection is not updated. What is the best way to synchronise these two collections, bearing in mind my two ViewModel do not know about each other.

     

    Regards,

    Nei;

    Tuesday, August 24, 2010 2:50 PM

Answers

  • Hi,

    You can synchronize two collections by sibscribing to their INotifyCollectionChanged.CollectionChanged events. In the event handler, the arguments of the event will tell you which item was added, updated or removed. You just need to propagate that change to the other collection manually (note that you need to be careful so that the event handler for the other collection does try to fixup the first again, in which case you would end up in an endless loop).

    Possibly a better solution would be to use just one collection for everything. If you want the data to be shared, then it kind of offers itself to share the datastructure as well.

    What other problems do you see with WCF Data Services?

    Thanks,


    Vitek Karas [MSFT]
    • Marked as answer by edhickey Thursday, September 9, 2010 5:39 PM
    Wednesday, September 1, 2010 4:25 PM
    Moderator

All replies

  • Hi,

    You'll have to do this manually, the data service context doesn't have any events the collections could register to get notified when you add a new entity. You could register a handler on the INotifyCollectionChanges interface which both te collections implement to handle this.

    Thanks,


    Vitek Karas [MSFT]
    Wednesday, August 25, 2010 3:53 PM
    Moderator
  • Hi,

    You'll have to do this manually, the data service context doesn't have any events the collections could register to get notified when you add a new entity. You could register a handler on the INotifyCollectionChanges interface which both te collections implement to handle this.

    Thanks,


    Vitek Karas [MSFT]
    Thanks for the reply, I'm sorry but I didn't follow your suggestion. Could you give an example? Also could using a global static DataServiceCollection be an option or is this not recommended?
    Wednesday, August 25, 2010 4:09 PM
  • Hi,

    This issue is very urgent now, I may have to consider using plain WCF Services instead if I can't resolve this. I may have to abandon Data Services anyway because it really is not living up to expectations and has way too many problems.

    Neil

    Tuesday, August 31, 2010 4:39 PM
  • Hi,

    You can synchronize two collections by sibscribing to their INotifyCollectionChanged.CollectionChanged events. In the event handler, the arguments of the event will tell you which item was added, updated or removed. You just need to propagate that change to the other collection manually (note that you need to be careful so that the event handler for the other collection does try to fixup the first again, in which case you would end up in an endless loop).

    Possibly a better solution would be to use just one collection for everything. If you want the data to be shared, then it kind of offers itself to share the datastructure as well.

    What other problems do you see with WCF Data Services?

    Thanks,


    Vitek Karas [MSFT]
    • Marked as answer by edhickey Thursday, September 9, 2010 5:39 PM
    Wednesday, September 1, 2010 4:25 PM
    Moderator