Views accessing other controllers

Asked

Viewed 275 times

1

Let’s say I have the class Veiculo and the class Fabricante. Each vehicle has a manufacturer as an attribute, among other attributes.

I have the views to maintain (register, edit and delete) and search vehicle and also manufacturer.

Each model class has its own controller (I decided to create 1 control per model, instead of creating a view). That is, the class controlVeiculo will be accessed by more than one view and will have the methods

+listarVeiculos()

+salvarVeiculo(Veiculo v)

+editarVeiculo(Veiculo v)

+excluirVeiculo(Veiculo v)

(...)

Which in turn accesses the DAO classes. As well as the class controlFabricante will have the same methods as manufacturer.

When I call the view to register vehicle, for example, I will need to list manufacturers already registered in the bank for the user’s choice. That list of manufacturers must come from where?

  • create an instance of controlFabricante within the viewVeiculo and call the method normally?
  • leave mine viewVeiculo only by accessing your control (controlVeiculo) and create a method in controlVeículo to access the DAOFabricante?
  • leave mine viewVeiculo only by accessing your control (controlVeiculo) and, instead of creating a method in the controlVeículo, I create an instance of controlFabricante inside controlVeiculo and call the method between the controllers (since it already has a method defined in the other control, avoiding duplicating methods)
  • any other suggestions?
  • You instantiate a manufacturer model and call the method to list them,

  • Without going through any control even? Instancio o fabricante dentro da viewVeiculo?

  • Yes, without any controller, unless you were validating data in the controller, because normally the model is responsible for validating and saving/editing/erasing/fetching data in persistence. And no, you create an instance in the vehicle controller, in the list method, so you take the result of that instance and send it to the view

  • Blz, thanks for your help!

  • @Gustavobelczak The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how to do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

5

I will answer based on what you have in the question. I may have other information that makes me think differently.

I don’t think you understand the MVC. You don’t need to have a one-to-one relationship with anything, the MVC exists precisely not to have the one for one, if you have then you should not use the MVC.

Create the templates you need to manipulate the data, possibly by making persistence.

Create the views you need to present the data, possibly each one will be a screen or part of a screen, however it is shown to the user.

Create the controllers you need to do various operations. A controller does not need to be connected neither to the vision nor to the model, it must perform something that makes sense to be in a single unit.

You can have a controller only for what you want. Or you can have the operation you want inside an existing controller if it makes sense.

Take that relationship out of your head and it all starts to clear up.

In general the controller should only make technical operations of the application. It should make the assembly of what is necessary to offer to the view, normally asking for data to the model.

Who should provide data is the model.

That list of manufacturers must come from where?

Of the manufacturer model.

create an instance of controlFabricante within the viewVeiculo and call the method normally?

No, the layers should be independent, and the view must receive data passed by controller.

leave mine viewVeiculo only by accessing your control (controlVeiculo) and create a method in controlVeículo to access the DAOFabricante?

More or less that. Creates an instance of model within the controller to receive the data, process and send to view.

leave mine viewVeiculo only by accessing your control (controlVeiculo) and, instead of creating a method in controlVeople, I create an instance of the controlFabricante inside controlVeiculo and call the method between the controllers (since it already has a method defined in the other control, avoiding duplicating methods)

I’m not saying you can’t do this, but you need to see if it makes sense.

Maybe I need to do something different from that, but on something generic, no details it’s hard to say.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.