1

I've started learning MVVM pattern and face some issues. I'm building an application which stores a list of provinces and cities of a certain country. Now I need to know how I should exactly implement the models.

I should say that the tables and their fields in the database are as follows:

table: Provinces: ID, Name

table: Cities ID, ProvinceID, Name

Now I wonder which approach is better for the models? Should the property of the models exactly the same as the fields in the table? for instance:

class Province {

public uint ID {get; set;}
public string Name {get; set;}

};

class City {
    public uint ID {get; set;}
    public uint ProvinceID {get; set;}
    public string Name {get; set;}

}

Or is it better for the class City to have a reference to its province, for example:

class City {
    public uint ID {get; set;}
    public Province Province {get; set;}
    public string Name {get; set;}

}

or any better ideas?

What about the viewmodels? should I have a list of CityViewModels in the ProvinceViewModel?

Thanks in advance. Please let me know if I've not clearly explained my issues.

user3530012
  • 720
  • 2
  • 20
  • 37
  • I would say you need a more understanding of MVVM model. I would have a look at this post which recommends few great resources. You can find your answer through the examples. http://stackoverflow.com/questions/1405739/mvvm-tutorial-from-start-to-finish/2034333#2034333 – Mehrad Apr 13 '14 at 22:51

1 Answers1

1

Why use an ID to a specific Province if you can have the actual Province as a member of your class?

Imagine you need the name of the province, therefore you will need to locate and construct the Province first to get the actual name. That being said, it is not necessary at all.

Basically you have a Model, a ViewModel and a View in a MVVM pattern. The View interacts with the ViewModel and vice versa. Plus, the ViewModel uses the Model in order to make changes.

You could use both of your classes as Model while using a ViewModel for each class. The ViewModel for Province could be constructed as follows:

class ProvinceViewModel : INotifyPropertyChanged
{
    private Province _Province;

    public string Name
    {
        get { return _Province.Name; }
        set
        {
            if (_Name == value) return;
            _Province.Name = value;
            OnPropertyChanged("Name");
        }
    }

    public ProvinceViewModel(Province province)
    {
        _Province = province;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

Notice the implementation of INotifyPropertyChanged. You may now ask why I implemented that. In order to update your View, you need this interface to publish any messages and to inform your View, that something in your Model has changed.

How about using a framework such as Caliburn.Micro? That will, of course, safe you a lot of work and feels very comfortable once you get into it.

0x8BADF00D
  • 962
  • 1
  • 8
  • 18
  • Cannot we just have a Province member in the ProvinceViewModel rather than each single member of the Province model itself? Actually I've seen in the tutorials that a Province member is in the ViewModel called CurrentProvince or SelectedProvince. Which way is more correct? – user3530012 Apr 14 '14 at 08:53
  • In order to notify the View that a member of your model has been updated, you need to implement each member this way. That's required to update your view constantly without doing that manually. You can name things as you want but I guess that these were called `Current` or `Selected` because they are actually representing the current or selected item of a control such as DataGrids or SplitButtons. – 0x8BADF00D Apr 14 '14 at 09:13
  • I understand that but why not models inherit INotifyPropertyChanged? – user3530012 Apr 14 '14 at 09:21
  • See http://stackoverflow.com/questions/6922130/in-mvvm-model-should-the-model-implement-inotifypropertychanged-interface for more details on this – 0x8BADF00D Apr 14 '14 at 09:28