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?
Sharing Data between Silverlight ViewModels
Asked Answered
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.
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.