For my WPF application, I chose MVVM. Here is my concept of how I will implement this template.
- My models (business objects) are responsible for checking (which is mandatory for me).
- ViewModels are responsible for packaging my model for user friendliness and some security aspects.
My first question was about porting or not porting my model to ViewModel.
I decided that I ViewModeldefinitely should wrap the model. Thus ViewModelimplements INotifyPropertyChanged.
But now I have a problem with checking the business.
IDataErrorInfo, - ViewModel, . - .
. A, 1 2 . B, 3 - , "" , . , / DateTime-Ranges.
, ViewModel, - -.
?
:
ValidationRules ,
public string ValidateBirthday(string birthay)
{
if (...)
{
return "Birthday should be…";
}
return string.Empty;
}
ViewModel IDataErrorInfo :
public string this[string columnName]
{
get
{
switch (columnName)
{
case "Birthday":
return Model.ValidateBirthday(Birthday);
case "XXX":
return Model.ValidateXXX(XXX);
case "YYY":
return Model.ValidateYYY(YYY);
break;
}
}
}
- ( ) , .
- ?
, ...
INotifyPropertyChanged IDataErrorInfo .
.
:
PersonViewModel: INotifyPropertyChanged {private Person _personModel; public Person PersonModel {get {return _personModel; NotifyPropertyChanged(); }}}
public PersonViewModel(Person person)
{
PersonModel = person;
}
…
}
:
<DatePicker Text="{Binding PersonModel.Birthday}"/>
, WPF .
: 20.07.2008 , PersonModel , PersonModel , , PersonModel => SaveButton .
"aaa" , WPF , (DateTime). PersonModel , PersonModel => SaveButton !
"" ViewModel .
ViewModel :
PersonViewModel: INotifyPropertyChanged {private Person _personModel;
public string Birthday
{
get
{
if (_personModel. Birthday!= null)
{
return ((DateTime) _personModel. Birthday).ToShortDateString();
}
else
{
return String.Empty;
}
}
set
{
if (_personModel. Birthday.ToString() != value)
{
DateTime dateValue;
if (DateTime.TryParse(value, out dateValue))
{
_personModel.Birthday = dateValue;
…
}
else
{
…
}
}
}
}
public PersonViewModel(Person person)
{
_personModel = person;
}
…
public string Birthday
{
get
{
if (_personModel. Birthday!= null)
{
return ((DateTime) _personModel. Birthday).ToShortDateString();
}
else
{
return String.Empty;
}
}
set
{
if (_personModel. Birthday.ToString() != value)
{
DateTime dateValue;
if (DateTime.TryParse(value, out dateValue))
{
_personModel.Birthday = dateValue;
…
}
else
{
…
}
}
}
}
public PersonViewModel(Person person)
{
_personModel = person;
}
…
}
View. ViewModel, .
<DatePicker Text="{Binding Birthday}"/>
: , . 'aaa' Datepicker, => SaveButton .
, View Model. . Model get set , Model get set. ViewModel, get. .
, ViewModel, IDataErrorInfo ( )?