Sharing Data between Silverlight ViewModels
Asked Answered
D

1

0

I have a complex User Control that contains some views, each have its own ViewModel. My question is how can all of these ViewModels share some data (for example an observable collection) without each one have a separate call to the service?

Disinfest answered 13/12, 2010 at 14:20 Comment(0)
W
0

The service should be an abstraction of the data. Whether that data is pulled from a WS, DB, etc...should be irrelevant. Each ViewModel can contain a property which will be bound to by the View. That property can be an ObservableCollection<T> which wraps a call to the service. That data may in fact be cached via the service and only update periodically but either way it will push the data to a single point of reference for retrieval amongst the ViewModels.

Workroom answered 13/12, 2010 at 14:32 Comment(2)
Thanks Aaron, when you say "single point of reference", do you mean the service should be implemented as a static class?Disinfest
@Assaf it can depend...but in your instance of wanting to make use of caching a singleton in some form or another would be needed; thus making your service static would be one option. If you were using Prism or varying framework you can set the lifetime of the object to behave like a singleton so that when you Resolve a type it will provide the same instance to the requester.Workroom

© 2022 - 2024 — McMap. All rights reserved.