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?

  • In the PersonViewModel, assuming that's the data that will be passed to the server when the "save button" is pressed. – Robert Harvey Mar 26 '17 at 17:32
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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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