1

The scenario:

I have a GUI with textfields and radio buttons where the user insert information for a 'Person'

Here are the classes of the model and of the view-model:

enum Gender 
{
    case Male, Female
}

class Person
{
    var name: String = ""
    var surname: String = ""
    var id: String = ""
    var gender: Gender?

    setDataFromViewModel(model: PersonViewModel)
    {
        // doing stuff
    }
}

class PersonViewModel
{
    var name: String = ""
    var surname: String = ""
    var id: String = ""
    var gender: Gender?

    init(person: Person)
    {
        // doing stuff with the strings to make them nice
    }
}

If I already have the data the PersonViewModel will get it from the Person and give it to the view.

If the user in writing the data the PersonViewModel will get it first, and pass it to the Person model.

Questions: If I have to check integrity of the strings coming from the View, where should I do it? (checking that the string is not empty, that the ID match a certain regex, etc).

Is this an optimal implementation of the pattern ora am I missing something?

1
  • In the PersonViewModel, assuming that's the data that will be passed to the server when the "save button" is pressed. Mar 26, 2017 at 17:32

1 Answer 1

0

Based on your requirements and business logic.
If by "integrity" you mean formatting logic then this logic must be in the viewmodel, because responsibility of viewmodel is "prepare" data for the view. Same for input, viewmodel responsible for "converting" formatted data from view to the "business logic" format.

But if you need check for integrity based on some "business logic" (name must be unique or id must be in some range), then this validation need to done in the model or at least provided to viewmodel as service

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.