Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should controller methods take arguments?

Given that there is file selection widget on the view and controller need to handle event of selecting file, should I rather write controller method:

public void fileSelected(String filePath){
  //process filePath
}

or

public void fileSelected(){
  String filePath = view.getSelectedFilePath();
  //process filePath
}

The first approach seems to introduce less coupling between C and V: C don't know what exactly data does C need while handling given event.

But it requires creating a lot of verbose methods similar to getSelectedFile on V side.

On the other hand, second approach may lead to cluttered controller methods in more complex cases than in example (much more data to pass than just filePath).

From your own experience, which approach do you prefer?

like image 480
Piotr Sobczyk Avatar asked Oct 27 '12 07:10

Piotr Sobczyk


People also ask

How to pass parameters to action method in MVC?

There are a number of ways in which you can pass parameters to action methods in ASP.NET Core MVC. You can pass them via a URL, a query string, a request header, a request body, or even a form. This article talks about all of these ways, and illustrates them with code examples.

Can we pass data from view to controller in MVC?

This blog will discuss four (4) common ways to pass data from the view to the controller: Passing by Typed Arguments. Request Object. Form Collections Object.

What will happen if you Cannot parse the parameter value of an action method?

Action Method Parameters If the parameter value cannot be parsed, and if the type of the parameter is a reference type or a nullable value type, null is passed as the parameter value. Otherwise, an exception is thrown.

What are Controllers in java?

A controller basically controls the flow of the data. It controls the data flow into model object and updates the view whenever data changes.


2 Answers

The first approach is my favourite. The only difference is I would rather use an object (like Mario suggested) to pass arguments to the method. This way method's signature will not change when you add or remove some of the arguments. Less coupling is always good :)

One more thing: If You want to try the second solution I recommend using a ViewFactory to remove view logic from the controller.

like image 115
op1ekun Avatar answered Oct 02 '22 23:10

op1ekun


The first approach is the way to go;

public void fileSelected(String filePath){
  //process filePath
}

The Controller should not care about how the View looks like or how it's implemented. It gets much clearer for the developer as well, when creating/updating the view, to know what an action in the controller wants. Also it makes it easier for method overloading.

Though, I don't know really how String filePath = view.getSelectedFilePath(); would work. Are we talking about parsing the View code/markup?

On the other hand, second approach may lead to cluttered controller methods in more complex cases than in example (much more data to pass than just filePath).

That's when you would create a View Model class (let's say we name it MyViewModel) to store all the properties that you need to send (may it be 10 properties) and then pass that in the action: fileSelected(MyViewModel model). That's how it's intended to be used and what the *ModelBinder's in asp.net mvc are there to help you with.

like image 21
Mario S Avatar answered Oct 02 '22 21:10

Mario S