Log in

Explaining “The ViewModel does not require a root object” warning

From MDrivenWiki

ViewModels can either take their starting point in an Object you provide as a root or not. If they do they typically make use of that object – something like this:

Explaining -1.png

The assigned root object is accessed with “self” on the green Root ViewModel class above.

But a ViewModel may also be constructed as not having the need for a root object – this is the typical case for Seekers or Browsers that look thru everything in class. The Browser pattern is very common for managing small value stores. Like this:

Explaining -2.png

When using this pattern – you do not use “self” in the Root-ViewModel-class.

Since both these scenarios are legit it easy to imagine situations where you “forget” the intention of a ViewModel – was it Rooted or was it not?

If it was intended to be rooted, the action bringing the viewmodel up really should supply a root object like this:

Explaining - 3.png

But if it was not intended to be rooted – as in the ViewAllThings case – then it would be no point in submitting a RootObject from the action:

Explaining - 4.png

To clarify your intentions it is good style to check the “Requires root”-checkbox:

Explaining - 5.png

Because if you make your intensions clear this way – the environment can help you to make sure that you follow thru.

When you have a ViewModel that RequiresRoot – but you have an action that does not supply a RootObject you get a Warning:
“WARNING: If the ViewModel requires a root object [RequiresRootObject on ViewModel] you should not have it empty ViewOneThing”
And if you have a ViewModel that does not Require a Root – but an action submits a RootObject you get another warning:
“WARNING: The ViewModel does not require a root object [RequiresRootObject on ViewModel] but there is a Expression in ViewModelRootObjectExpressionViewOneThing”