21

MVC is pretty straightforward. There is a Model, a Controller and a View. When we create a website, it all come together as 'client sends REST keyword request to server -> the server matches the requested URL to the controller action -> which then calls the model(s) for data gathering/processing, gets the result -> and returns the result back to the client as a HTML page (view)'.

What if we are talking about a pure RESTful API web service? Then the flow with be something like 'client sends REST keyword request to server -> the server matches the requested URL to the controller action -> which then calls the model(s) for data gathering/processing, gets the result -> and returns the result back to the client in JSON'. Same as before, but there is no 'view'...or rather, the generated JSON can be thought of as a 'view'. In a sense, we are only utilizing the MC part of MVC. Is that how it is should be done? Or are there any other, better-suited patterns for a API-only service instead of MVC?

1
  • so these days, I tend to believe that OOP itself, along with the so-called "design-patterns" are cancer
    – simon
    Commented Jul 10, 2022 at 9:56

4 Answers 4

32

MVC is a paradigm from the Smalltalk world concerned with how object orientated systems could have UIs.

Early web frameworks took the general idea (separate out business logic, controlling logic and view logic) and applied the principle to how they structured the web application. Before this it wasn't uncommon to have a mess of HTML generation code inside domain objects, or business logic inside HTML templates (think very early PHP)

The thing is that the original MVC from the Smalltalk world isn't really what MVC is in most web frameworks. A HTML output isn't really a "view" in the sense that Smalltalk understood a UI screen to be.

So that is the first reason not to get too hung up on whether you are following MVC properly. Hardly anything is. Take it less as a strict division and more a guideline of Hey wouldn't it be nice if our HTML templates weren't full of business logic.

Secondly MVC is just a way of structuring server side code. It really has nothing to do with REST/HTTP. REST is concerned with how clients and servers communicate. It doesn't care if the representation the server sends to the client is in a HTML template that took a lot to construct with a templating engine, or a JSON object that was one call in the controller.

If you don't think your server needs a "view" layer, that is fine. You will still gain benefit separating out your business logic (ie model) from the controllers that are handling a specific HTTP request, even if all the controller does it call a JSON parsing call on some object and return that data.

5
  • 1
    Exactly what I needed to hear. I come from the web dev world (along the one-file-scripts), so I haven't seen how non-web large scale apps are ususally structured. The system I am currently implementing goes way beyond a regular web app. So from what I've read so far, it doesn't really matter how you structure the app source, the main goal is to make it easy to navigate and maintain. I will use concepts from MVC pattern to structure my app. Thanks!
    – simon
    Commented Jul 18, 2016 at 11:31
  • @lime ...the main goal is to make it easy to navigate and maintain. Isn't that always the goal?
    – Andy
    Commented Jul 18, 2016 at 11:34
  • @David Packer of course it is =) I was just too locked onto a concept, missing out on the real use of that concept.
    – simon
    Commented Jul 18, 2016 at 12:55
  • 1
    Check out Bob Martin's presentation on Clean Architecture if you want to see a different, potentially better way to structure an application than the way many web frameworks do. Commented Jul 18, 2016 at 13:00
  • 1
    Don't forget that JSON, XML, and HTML are just different representations of the same resource. So whether you are straight serializing the object or generating HTML from it doesn't change the fact that they are all technically different views for the same data. It's not like you are sending the object itself, just a copy of it in a different format. Commented Aug 31, 2021 at 13:25
11

View is a layer responsible for displaying information which may be interpreted by a user/client of your application (it does not say the user has to be an actual person). JSON is completely valid format for a view layer, computers understand that.

As long as the view layer publishes information which can be used by a user to affect models in your application, it does not matter how the view looks like, it is still a view, a layer acting as a middleware between the user and the system.

1
  • 1
    Good point, I suppose it's worth pointing out that HTML by itself is just a string of text organized as a bunch of tags, not that different from XML or even JSON; it's then rendered by browsers according to some standard but as far as the backend is concerned the view layer is just structured text
    – Alexandre
    Commented Dec 13, 2022 at 17:11
3

MVC is pretty straightforward.

Martin Fowler would, perhaps, disagree with this:

Different people reading about MVC in different places take different ideas from it and describe these as 'MVC'.

Moving on...

When we create a website, it all come together as 'client sends REST keyword request to server -> the server matches the requested URL to the controller action -> which then calls the model(s) for data gathering/processing, gets the result -> and returns the result back to the client as a HTML page (view)'.

OK, this is a bit of a tangle

MVC, whatever it is, is a collection of ideas for implementing user interfaces.

REST is a collection of architectural constraints for building out large scale applications.

The web, which is what you are talking about here, is a giant document management application built using most of those same constraints.

The similarities you are seeing between the two are (take your pick) incorrectly attributed, or superficial.

RESTafarians have a common understanding of HATEOAS, "hypertext as the engine of application state", and that should send alarms ringing through you head -- why would a view be an engine of state? If we question the premise, and look for additional evidence, we might also notice two odd things.

First, that we can take the HTTP server completely out of the equation by loading the HTML from disk. The browser is perfectly content with this, excusing some minor variations in behavior that may arise from the change in base url. Views don't generally continue to work when they have been completely disconnected from the model and controller like that.

Second, if we observe a modern browser carefully, we'll notice that there are multiple views of the HTML. Multiple views of a view seems like a really strange idea, but sure enough there's the main presentation, with a bunch of text markup that responds to user gestures, and then there's this "Source View" thing that shows the raw HTML and also responds to user gestures. It's turtles all the way down!

The answer to the riddle, of course, is that the HTML isn't the view. The collection of widgets in the browser are the view, and they are in communication with the Document Object Model, which was initialized by reading the HTML.

In other words, the HTML is a representation of state, just like Roy T. Fielding promised.

What if we are talking about a pure RESTful API web service...? Same as before, but there is no 'view'

More correctly, same as before: there is no view. The JSON, just like the HTML, is a representation of state, suitable for crossing process boundaries.

Think "DTO" or "Message" and you're inferences will be much less likely to lead you astray.

2
  • I've mixed web requests with an architectural pattern to more easily illustrate what bothers me in the concept as a whole. You're saying: "The collection of widgets in the browser are the view" - then I rephrase: what if there is no 'browser' in a human scense? What if it's another robot connecting to the service? If JSON and HTML are the representation of state, then 'a message' or 'DTO' is a transport for state representation. So where does 'a view' come in place then? You've confused me even more with your answer.
    – simon
    Commented Jul 14, 2016 at 8:16
  • The program/robot connecting to the service would presumably manipulate the model directly - why would it need a view? Commented Jul 14, 2016 at 13:07
1

Is that how it is should be done?

Passing the JSON as a view, or using it as a view model to construct the view does not violate the pattern.

I am using the same architecture in the current application I am working on and it is working very good. Together with some nice JS framework you can create some really responsive designs.

Or are there any other, better-suited patterns for a API-only service instead of MVC?

Honestly, no idea. But I think that whether you use MVC or not in an API is not that important. Use whatever you find convenient. When talking about web services there are a lot more important aspects to consider (which are not directly related to MVC), e.g. security, consistency, availability, etc.

Your Answer

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

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