Where to place the timer in a mvvm project that polls a feed
I have a MVVM p开发者_JAVA技巧roject that polls a feed every 10 seconds to see for updates. The result from the feed is displayed using a ObervableCollection. This works nice, but Im unsure as to where I can put my dispatchedtimer. Should it be in the Model.DataService or in the ViewModel.MainViewModel?
thanks
I think there is no right answer possible, without knowing more details about the project.
Regarding the MVVM-Pattern:
Do you think it's a matter of your BusinessObjects? (Yes => Place it in Model.DataService)
Or is it a feature of your View? Yes => Place it in the ViewModel.MainViewModel.
Are you going to develop a view that displays all the available updates at the moment the user push the button (Show me updates)? If so, I suggest to place the timer in the ViewModel layer.
I asume that your view is open the whole time and is bound to an Observable Collection that resides in you ViewModel. In that case I would definitely put a time in the ViewModel because the ViewModel is the owner of the Observable Collection.
This is a perfect situation to use the Reactive Framework using the Observable.FromEvent method. Rather than polling, this will allow you to set up a subscription to the event. You can even trottle it (.Throttle(3000) for a 3 second delay), ignoring events until the time has passed.
精彩评论