Why is MVC used on the server? Isn't it a client-side pattern? - model-view-controller

I'm trying to learn web development.
I understand (mostly) the concept of MVC, but I'm confused about why an MVC model is used on the server side...like Spring MVC. Isn't the server side the Model and Services, and then the client side Services, View, and Controller (AngularJS even makes that pattern explicit on the client side)?
I'm really struggling with how the MVC model fits into or facilitates server-side development.

MVC is a pattern used by much more than just web applications. Any app with a UI could use an MVC pattern.
The idea is that you have a View (html, or a window in your OS, or even a report or something), and you have a model that represents the dynamic parts of that view. Then you have a controller that is dedicated to processing input and doing the "business logic" to generate the model and apply it to the view.
So.. for example on the Server you might have this MVC pattern:
A controller receives the HTTP request and processes it.
It builds a model
The model is applied to a view to generate HTML and send it back as a response.
On the client it will be similar (but a bit different in Angular's case):
A controller is used to determine and manipulate the model.
The model is then bound to your view via directives. (Angular is really more of an MVVM pattern, but it's similar enough)
The view is similarly bound to your model via directives. (this is where the MVVM part comes in).
The idea here is that both the model and the view are kept up to date by directives.
The controller just contains "business logic" for manipulating the model.
Clear as mud?
No worries. Just know this: It's just a common pattern. It's not "server specific" or "client specific". It can be used anywhere by anything requiring data to be scrubbed into templated output.
EDIT: More thoughts.
In the case of a Web API that serves up JSON (or even XML) on the server, you're still using MVC in most cases. This is because what you're doing is:
Process the request in a controller.
Build up the model in the controller.
Render the model to a "view", which in this case is a view that serializes it out as JSON.

In the good ol' days of yore, the client side was only a display. The server was responsible for communicating with the model, applying business logic, generating a view, and sending the static, rendered content back to the client (browser).
As the web matured, some of those responsibilities migrated from the server to the client. Now, the server-side is often a thin layer like RESTful API that stores the "official" business logic (rather than convenience logic on the client) and stores the model. But for performance and user experience, the client now stores a copy of the model in its own model layer, communicating with the server and/or local storage as necessary, and having its own controllers and view logic to provide an awesome user experience.
So does MVC still apply on the server? Yes! It's just different. The server often generates the initial view from which the client-side application runs (taking localization or internationalization into account, for instance) and still houses the official model. But more importantly, the "view" in MVC just changed. Instead of the server-side view being HTML, it's now JSON or XML that the client application consumes instead of just renders.
So for functionality's sake, we still use MVC on the server. But for an awesome user experience, we use MVC on the client-side now too.

Related

Ember: What is the accepted paradigm for saving data to the back-end?

I am new to Ember and am used to the MVC model of creating controllers to move data between the view and the server. With Ember, it appears that controllers are support but there is very little documentation on them on the Ember website. From what I can tell, there is an alternative method to save data through the route. Is there an accepted paradigm for how to save data from the front-end to the back-end in Ember?
The Ember guides show you how to use different alternatives.
One easy way is to use Ember data to handle the rest interface with the backend.
Regarding architecture design, you can use the routes or the controllers to interact with you backend using the store.
In our projects we retrieve data in the route, and store it from the controllers.

Calling a WebApi as a class instance

This is more of a design question than a problem. So here's the scenario, you have an asp.net 5 application with a webapi controller and it provides data to many types of clients: web, ios, java apps, etc. Let's say that one of those clients happens to be an mvc controller within the same web host and visual studio solution as the webapi.
What are the ramifications of calling into the webapi as a class instance, instead of doing what the other client types are doing--which is to make a rest based network call? The obvious benefits are eliminating the over head of a network call and eliminating the serialization. But I wanted to know what some of the possible negatives could be. Has anyone done this before?
The easy solution could be that you could extract that logic to a separate assembly... let say a "Business Logic Layer" so that both WebApi and MVC could access it.
This has the downside, that you will not be able to have MVC and WebApi separate. I mean, using WebApi as a single data interface could allow you to host MVC app separately from where the WebApi is hosted... but the approach in the first paragraph will couple both proyects and will force you to host them together to have access to the data.
If you call the class directly any work done by the HTTP pipeline won't be done. So your API class won't have access to the HttpContext for example.
Also none of the security or Http related annotations (attributes) will work, so your MVC controller may need to deal with that.

Where is an API located in the MVC architecture?

To which part of the MVC pattern belongs an API? Am I right with the assumption that it belongs to the view?
I'm talking about an application which provides an API to access data from the model.
Then these are conceptually different things. You are offering an API on your server (let's assume REST based); the API here is your server and the software which runs on it. That software may internally be built using an MVC pattern. An API request is handled by a controller, a model, and the response is output by the view. An API request uses all three parts of MVC. "The API" is what your software looks like "from outside".
If you consider the server you are connecting to through the API as a data storage, the API is a layer between the data storage and the controller/view.
In MVC architecture, the model contains a data storage with a service that controller or view can access to obtain the data.
In this case, API is the service, so I would say it is part of the model.

Why Smalltalk-80 MVC cannnot be applied to web development?

I have read in some places that Smalltalk-80 original MVC is not applicable to web application development because (1) it was made to develop GUI components, not an entire application and (2) because model cannot communicate with view.
(1) is valid, but I do not understand (2). First, Smalltalk-80 MVC does not says that model communicates directly with view, it does so using events. Second, I have an assumption that only controller would be able to alter the model. Then, in a http request, controller layer alters model layer, which raise events which could be observed by view layer. The only diff to controller->model->controller->view (MVP - passive view) is how model notifies the view of its changes. Could someone clarify?

Is Asp.Net Ajax only used at presentation layer, or also at Business Logic layer?

Is Asp.Net Ajax only used at presentation ( UI ) layer, or also at Business Logic layer?
EDIT - to be more precise, is AJAX API also used at BLL layer?
thanx
AJAX Should Only Apply To Presentation Layer....
You must remember that AJAX allows the client (UI/Browser) to make calls back to the server to do something. At which point your business layer should be invoked to get/retrieve data or do something...
So when it comes to a decision on whether or not to add AJAX functionality to a website, you will most likely do it for some UI/Presentation Layer related desired functionality. And as soon as you do add it, your business layer will be used to serve up the information needed to the client.
The AJAX technologies itself do not necessarily apply to the business layer and I would argue that they should not. The AJAX should simply get you to a place in your code where your front end (UI Code) can work with your business layer code.

Resources