I’m confused about when I should use a strongly typed view model vs a JSON object when passing object between the view and controller layers. My understanding is that using a JSON object follows the MVVM pattern, and keeps the layers loosely coupled. Any feedback would be appreciated.
Share
There are pros and cons to both approaches.
On the one hand, loosely typed means you can pass the same type all over the place and consumers can just consume the fields they want, if they exist. This makes your program appear simple, because it has such a simple architecture.
On the other hand, that means you push interpretation of fields to the consumer, and if you have complex data types this can quickly become a nightmare for the consumers to handle.
Make no mistake: you’ll deal with type safety somewhere. The choice is yours as to where.
I personally prefer strongly typed systems with well-defined interfaces, and so that is what I recommend.
EDIT
If you are making a REST webservice, you should return JSON from your View, by all means.
But internally, I would argue that you’re best off using strongly typed data types that are serialized to and from JSON.