WPF, MVVM, validated business object not very well matched

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.

  • When I do not wrap my model in the ViewModel and do not expose the model directly to the view - then I don’t understand why I need the ViewModel (this seems pointless)
  • ViewModel should wrap the model for various reasons:

    1. I do not like the direct binding to strongly typed properties in Model (DateTime, int, ...), because when I do this =>, WPF controls my validation for these types. This is really bad, because when a user writes' aaaa in Datepicker, my Model is valid (my model never knows about it, because WPF takes control of strongly typed properties), and the Save button is turned on, which is really wrong.

    2. I do not show all the properties of my Model for presentation, mine ViewModelshould protect my Model (I have some properties that should only getter and not setter at the presentation level)

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 .

.

  1. :

    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 .

  1. 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 ( )?

+4
1

: Bussiness .

-, .

:

  • , , - , .

    /li >
  • , , .

:

  • - (BO) - , , .

  • (DTO) - , .

  • ViewModels , DTO.

, - , - .

, , . , .

Save , ViewModel.

, SOLID : ( → , → , dto → , , viewmodels → ). , .

1- 2- : UI : , , ..

, " , " , :

  • "".
  • .
  • DTO .
  • Backend DTO .
  • Backend .
  • .

: .

: DTO - , backend, WCF, , , ( ). , .

, , .

+3

Source: https://habr.com/ru/post/1539407/


All Articles