1

For example, I have an UI with a button "Get Items" and a text field "Order". Then my button will retrieve all items from the order written in the text field. How should my controller know the order? Should it be

public void GetItems(string order){

}

or

public void GetItems(){
    string order = View.Order
}

or maybe neither?

5

I would go for option 1, since your controller does not need to know about your view to do his thing.

If you chose option 2 your controller needs to know about the view. If there are changes to it, you need to change the controller too.

Another advantage would be, that you can easily switch your UI. In the new UI you can call the method in your controller, without having to change the controller itself. Or maybe you want to go without UI and use a webservice to call your controllers methods.

So, to answer your question: Your controller should receive parameters, instead of accessing the view.

1

Really it boils down to, do you want to be able to alter the ordering externally?

So what I mean is; you have a page /MyController/orders. By default this is ordered ASC. If you want to have a link (somewhere else external or internal) that goes directly to the non-default ordering. Then I'd put this into the action as a parameter (option 1), this allows you (or anyone else) to call /MyController/orders?order=desc.

If this isn't neccasary then I'd hide this implementation detail (the more implmentation you hide from the end user the better, it just makes things appear neater) and pick option 2.

Don't gold plate your code. It's easy to switch between the two so fulfil your current needs and no more. So if you think option 1 might be needed at some point choose option 2. You can always alter to include option 1 when and if you need it later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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