Rating switch in MVVM chapter

Why is the switch on rating done in the view controller instead of the view model? My understanding is that ViewModels should do these sorts of transformations. Or am I mistaken?

@jstrawn Can you please help with this when you get a chance? Thank you - much appreciated! :]

Good question!

So a ViewModel is an class for translating an object’s properties into something that a view can display. The BusinessMapViewModel class is taking YLPBusiness gotten from the Yelp API and transforming them in MKAnnotations - something Apple’s map view can actually work with.

The switch case in the View Controller is playing a different role because it’s just setting the image of the view model. You’re right in that this doesn’t need to be done in the View Controller, which is why in the next chapter, Factory Pattern, we put this logic into a Factory class to get it out of the View Controller.

For this app it could totally make sense to put the switch case into the view model, but if the view model were to expand to include more types of places and businesses, it could be better to follow the single responsibility principle and set images in a Factory while transforming non-displayable types like YLPBusiness into displayable types in the view model.

Hope this helped!

1 Like