patterncsharpMinor
Simple clock view model
Viewed 0 times
simplemodelviewclock
Problem
I have been trying to wrap my head around MVVM for the last week or more and still struggling a bit. I have watched Jason Dolingers MVVM video and gone through Reed Copsey lessons and still find myself wondering if i am doing this right... I found both sources very interesting yet a bit different on the approach. If anyone has any other links, I would be interested as I would really like to learn this.
What is the best practice for the model to alert the viewmodel the something has happened? As you will see in the code below, I created a very simple clock application. I am using an event in my model but am not sure if this is the best way to handle this. The output of the program is as expected, however I'm more interested in if I'm actually using the pattern correctly. Any thoughts comments etc would be appreciated.
My model
My View
My View Model
```
using System;
using System.ComponentModel;
namespace Clock
{
public
What is the best practice for the model to alert the viewmodel the something has happened? As you will see in the code below, I created a very simple clock application. I am using an event in my model but am not sure if this is the best way to handle this. The output of the program is as expected, however I'm more interested in if I'm actually using the pattern correctly. Any thoughts comments etc would be appreciated.
My model
using System;
using System.Threading;
namespace Clock
{
public class ClockModel
{
private const int TIMER_INTERVAL = 50;
private DateTime _time;
public event Action TimeArrived;
public ClockModel()
{
Thread thread = new Thread(new ThreadStart(GenerateTimes));
thread.IsBackground = true;
thread.Priority = ThreadPriority.Normal;
thread.Start();
}
public DateTime DateTime
{
get
{
return _time;
}
set
{
this._time = value;
if (TimeArrived != null)
{
TimeArrived(DateTime);
}
}
}
private void GenerateTimes()
{
while (true)
{
DateTime = DateTime.Now;
Thread.Sleep(TIMER_INTERVAL);
}
}
}
}My View
My View Model
```
using System;
using System.ComponentModel;
namespace Clock
{
public
Solution
It seems your implementation is correct. You also touch a common discussion of MVVM.
In MVVM should the ViewModel or Model implement INotifyPropertyChanged?
One could argue you could let the model implement INotifyPropertyChanged. I'm not experienced enough with MVVM to answer this with a pro/contra argumentation.
The main intent however is implemented and the separation is there either way.
In MVVM should the ViewModel or Model implement INotifyPropertyChanged?
One could argue you could let the model implement INotifyPropertyChanged. I'm not experienced enough with MVVM to answer this with a pro/contra argumentation.
The main intent however is implemented and the separation is there either way.
Context
StackExchange Code Review Q#1771, answer score: 5
Revisions (0)
No revisions yet.