6
\$\begingroup\$

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

using System;
using System.Threading;

namespace Clock
{
    public class ClockModel
    {
        private const int TIMER_INTERVAL = 50;

        private DateTime _time;

        public event Action<DateTime> 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

<Window x:Class="Clock.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ViewModels="clr-namespace:Clock"
        Title="MainWindow" Height="75" Width="375">
    <Window.DataContext>
        <ViewModels:ClockViewModel />
    </Window.DataContext>
    <StackPanel Background="Black">
            <TextBlock Text="{Binding Path=DateTime}" Foreground="White" Background="Black" FontSize="30" TextAlignment="Center" />
    </StackPanel>
</Window>

My View Model

using System;
using System.ComponentModel;

namespace Clock
{
    public class ClockViewModel : INotifyPropertyChanged
    {
        private DateTime _time;
        private ClockModel clock;

        public ClockViewModel()
        {
            clock = new ClockModel();
            clock.TimeArrived += new Action<DateTime>(clock_TimeArrived);
        }

        private void clock_TimeArrived(DateTime time)
        {
            DateTime = time;
        }

        public DateTime DateTime 
        {
            get
            {
                return _time;
            }

            set
            {
                _time = value;
                this.RaisePropertyChanged("DateTime");
            }
        }


        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises the property changed event.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }
}
\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

Found this link which proved usefull to me:

http://karlshifflett.wordpress.com/2010/11/07/in-the-box-ndash-mvvm-training/

It explains MVVM and what each part of it is. I as well have been (and still am) searching for the correct ways to implement MVVM, but, as said, there are many different ways to go about. One of them is e.g. M-MV-V-VM; Model ModelView - View ViewModel, which I use @work. This is because the Model is actually an entity class (coming straight from the db, can't do anything about it), and so I opted to make a ViewModel for the model (wrapper class) and then implement INotifyPropertyChanged there.

Actually, come to think of it, my preference is to leave the model alone, entity or not. So as to have a clean as possible model.

\$\endgroup\$
1
  • \$\begingroup\$ I actually found this link yesterday, think it was mentioned in a video series i have been watching from NYC DevReady. I highly recommend watching this 5 part session. You can find the videos and code here blogs.msdn.com/b/peterlau/archive/2011/04/07/… \$\endgroup\$
    – poco
    Commented Apr 19, 2011 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.