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.

1
  • 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. stackoverflow.com/questions/1405739/… Commented Apr 13, 2014 at 22:51

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
I understand that but why not models inherit INotifyPropertyChanged?
See stackoverflow.com/questions/6922130/… for more details on this

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.