Controller belongs to the Presentation layer?
Asked Answered
F

4

17

I heard that controller belongs to the presentation layer. How is it possible?

I thought that :

  • view is for presentation
  • model is for business logic
  • controller is for controlling logic

Is there good link to prove that controller belongs to the presentation layer?

"Spring MVC is used for presentation layer": how can we use MVC only in the presentation layer?

Fussbudget answered 14/9, 2012 at 18:4 Comment(0)
B
24

The presentation layer contains views and controllers.
You must not mistake an MVC architecture for a multitier/layer architecture (especially a 3-tier architecture). Most of the time Model/View/Controller is not the primary design of a web application, it is just a subset of a multitier/layer architecture.

Take a look at this oversimplified scheme (you could have the DAOs in a dedicated Data Access Layer, but this is not important in this post) :

Simplified layers

Spring MVC is a presentation framework : it deals with controllers and views. But why the "M" in Spring MVC ? Just because, as many other presentation framework, it naturally deals with a representation of a model/entity ("M"). This representation is the one used in your controllers, displayed in your views, submitted in your forms, etc. That's why the framework is called Spring MVC, even if the model/entity is not part of the prensentation layer.

I think it is a good name for this Framework, because it is really "MVC" oriented. Indeed the representation of a model/entity can be :

  • direct : the framework directly handles the model/entity object
  • indirect : the framework handles a form object or DTO, that contains information related to one or multiple entities

Spring's recommendation is to directly use the model/entity ("M") object :

Reusable business code, no need for duplication. Use existing business objects as command or form objects instead of mirroring them to extend a particular framework base class.

That's why I say the framework is very "MVC" oriented, compared to others, like Struts, where you have to use different form objects.

Some interesting links :

Bali answered 15/9, 2012 at 21:16 Comment(2)
great help! I want to know how do Presentation Layer and Business Layer communicate? I know that UI and Controller interact through REST. Since Presentation and Business both the layers are in different machines (n -tier arc), does Service layer also accept REST calls? I have no real time experience so you may find this question lame, but it would be very nice of you if you educate me about it. Thanks in advance.Nodical
A model is not, and has never been, an entity.Chapnick
O
6

The controller controls the presentation layer logic. For all the business code, transactional use cases, persistence, etc., it typically delegates to a service layer.

A typical way of doing that is to implement transactional services as spring beans and inject those spring beans in controllers. Typical use case: create a new product:

  1. The controller receives a command bean from the browser
  2. It validates that all the required data is present, and if not, redisplays the product creation page with error messages
  3. It calls a service bean to create the product
  4. The service bean runs in a transaction. It gets the product category from the database, attaches the product to its category, computes the price for the product based on current pricing strategies, sends a JMS message to an external application, and returns the ID of the created product
  5. The controller redirects to the product detail page, using the ID of the created product as a URL parameter.
Oxonian answered 14/9, 2012 at 18:19 Comment(2)
so controller will be in between UI and service layer right? then how it will be in Presentation Layer?Fussbudget
UI and presentation are the same thing. The controller is part of the UI, or presentation, layer.Oxonian
P
4

It largely depends on what flavor of MVC you're using, and what environment you're using it in.

For example, ASP.NET MVC is entirely a UI pattern, so all three parts are part of presentation.

However, in most implementations of MVC, the Controller interacts with the user, and thus is part of the UI layer. It may handle button presses, and keyboard input... but in many cases, the controller is also responsible for connecting the Model and the View together.

The one universal truth is that you should NOT be doing business logic in the controller if you cannot help it. Where the business logic exists depends on many factors. It might be part of the model in some implementations, or it may be it's own separate layer outside of MVC

Prier answered 14/9, 2012 at 18:14 Comment(0)
W
0

Where do you get your quote Spring MVC is used for presentation layer from?

I think firstly its important to understand the meaning of MVC in context of Spring Boot.

In Sprint Boot the model represents the data. And therefor you handle any business operations there and not in the controller. The view represents the user interface!

The controller acts as an intermediary between the model and view. The controller receives input from the user, interacts with the necessary services or business logic to retrieve or manipulate data, and then prepares the response to be displayed by the view. Since that you can already say here, that it does not belong to the business layer since it should not do any operations on the data.

And why the controller belongs then to the presentation layer becomes even more clear since it

  • handles requests & responses
  • routes the requests based on their URL/HTTP Methods
  • does input validation and transformation
Wrasse answered 8/6, 2023 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.