Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In my experience, <code>Model</code> objects don't have to (and probably shouldn't) know that they are being constructed in a <code>View</code>. Often, <code>Model</code> objects are entities that should <strong>never be allowed to be in an invalid state</strong>. <code>ViewModel</code> objects are the things that construct the <code>Model</code> objects.</p> <p>So, since you never want to create a person who is very old or very young, and every person needs a name, your <code>Person</code> class might look like this:</p> <pre><code>public class Person { public int Age { get; private set; } public string Name { get; private set; } public Person(int age, string name) { if (age &lt; 0 || age &gt; 150) throw new ArgumentOutOfRangeException(); if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(); Age = age; Name = name; } } </code></pre> <p>And your <code>PersonViewModel</code> might look like this::</p> <pre><code>class PersonViewModel : INotifyPropertyChanged { private int _Age; private int _Name; public int Age { get { return _Age; } set { if (_Age.Equals(value)) return; _Age = value; RaisePropertyChanged("Age"); } } public string Name { get { return _Name; } set { if (_Name.Equals(value)) return; _Name = value; RaisePropertyChanged("Name"); } } public Person CreatePerson() { return new Person(_Age, _Name); } } </code></pre> <p>You can then put whatever values you want in your <code>PersonViewModel</code> without worrying about creating an invalid <code>Person</code> object. You can also implement validation in the <code>PersonViewModel</code> that may be more strict than the validation in the <code>Person</code> class (for example, restricting the age range to adults over 18 years old (see <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.idataerrorinfo.aspx" rel="nofollow">IDataErrorInfo</a>)).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload