I am currently working on a Windows Store application (8.1) which is supposed to do the following:
- Talk to a USB HID Device (figured that out)
- Display data from that device (I want to use Oxyplot for displaying this data, got that)
- Use MVVM (I selected SimpleMVVM toolkit since it already has templates for VS2013)
- Create a mock data provider which generates random data and feeds it to my ViewModel
Now I am kinda stuck here regarding where to put the data. I use a Queue to store my values (I always want the last 100 values displayed). Now, what do I put into the model and what do I put into the ViewModel.
E.g. would I put the Queue containing the data points into my ViewModel? How would I trigger the process "Get some data every 1 second" correctly. I thought of using a System.Threading.Threads.Timer for that. Where would I put that? Into the MockDataServiceAgent? In that case: How do I access my ViewModel from the ServiceAgent to execute the update?
Everything is fine if you have buttons and stuff, but what if you have random events which are effectively triggered by "something else" than the view?
Your
Model
is yourdomain object
, it represents the actualdata
orinformation
you are dealing with. An example of aModel
might be that of aCar
containing amake
,model
,colour
etc. The main thing here is that theModel
maintainsinformation
and not behaviour.The
ViewModel
is yourpresentation separation
layer, it can wrap one or more of yourModel
objects. It is the glue between yourView
andModel
exposing commands and methods which maintainView
state and can alter the state of yourModel
as a result ofactions
on theView
.Your
data
should be maintained by yourModel
, orModels
. It would be yourViewModel
which would expose thatdata
and provide a mechanism for yourView
to consume it. AnObservableCollection
is a common mechanism for exposing a collection ofdata
to aView
as it is dynamic and provides notifications when it has items added, removed or is refreshed entirely.Ideally you do not want your objects to have strong links to one another, so to communicate between objects you might want to consider some implementation of the Mediator design pattern. Most MVVM frameworks have some implementation of this either as a
Mediator
orEventAggregator
message bus. These provide apublish
andsubscribe
mechanism where one objectpublishes
a notification containing some data and one or moresubscribed
objects will receive that notification and process the data accordingly. None of the objects involved know who is apublisher
,subscriber
or who is involved, they only know of theMediator
implementation.