I'm learning the MVVM pattern and something that comes up often is a duplication of data-types.
Say I have a Person
datatype. Intuitively I want it to look like this:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public uint Age { get; set; }
}
But instead, the code will have a PersonViewModel
a PersonModel
class and in many cases there's also a PersonData
class which is used for serialization.
A PersonViewModel
inherits from a base class for ViewModels and the setters will have RaisePropertyChanged
calls.
public class PersonViewModel: ViewModelBase
{
private string _firstName;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
OnPropertyChanged("Person");
}
}
...
}
A PersonModel
does not call OnPropertyChanged
and does not inherit from ViewModel. Also one of the reasons for this is to not bind the view to the model directly, rather to bind it to a ViewModel.
A PersonData
will have the same properties but they'll be marked with the DataMember
attribute and the class will have the DataContract
attribute.
I have 2 questions:
1) Is it really necessary to have so many classes for the same data type? They usually have the exact same properties. Is it not better to just have one class per data type?
2) One of the problems is that changing one class requires you to change the other classes as well. I thought about using an interface that looks like this:
public interface IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public uint Age { get; set; }
}
and have the 3 classes implement the interface. If indeed all these classes are necessary is it a good solution for the consistency problem?
3) Is the distinction between "Model" and "ViewModel" really necessary in the data-types level? Sure I can understand why it's a good idea to have one unit of code be responsible for invoking commands, presenting data, etc... and have another unit of code be responsible for the business logic, but is it really necessary when we're dealing with data types that are being used across the entire codebase?
PersonViewModel
is there for the benefit of thePersonView
. The public properties exposed on that ViewModel are those required by the view itself, some or all of which may or may not correspond to the properties in thePersonModel
or the fields in the database.