wood burning stoves*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Quick question on MVC (DAO) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Quick question on MVC (DAO)" Watch "Quick question on MVC (DAO)" New topic
Author

Quick question on MVC (DAO)

Geoff Berl
Greenhorn

Joined: Apr 13, 2011
Posts: 24

I've been working on understanding MVC DAO for the past few days. One thing I'm still not sure of though is what part actually sets the data in my view?

I have a controller which is connected with a model and view
I also have a DAO that the model uses to get data from MySQL but I don't think that's relevant in this question.

When I want to add or update a list in a combo box, say initially or when a listener is triggered...
Does my controller call the model for the list and then put that list in the view?
Or does my controller call to the model to updateCombobox in the view?
Or, is it something different entirely.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
... what part actually sets the data in my view?


The Controller sends data to View. The Controller receives data to send to View from Model. The Controller sends data to Model from View.

I also have a DAO that the model uses to get data from MySQL but I don't think that's relevant in this question.


Objects used for data access are part of the Model. Objects that implement business/domain logic are also part of the Model.

The Model-View-Controller object-oriented design pattern does not dictate a specific implementation. There are a variety on implementation strategies that comply with the pattern's guidance. To clearly understand the pattern, you should understand what the fundamental objectives actually are, along with the benefits and limitations. This is very important knowledge to understand prior to analysis of various implementation strategies and designs.

When I want to add or update a list in a combo box, say initially or when a listener is triggered...
Does my controller call the model for the list and then put that list in the view?


If the data for the list is stored in a database, yes, the Controller calls upon the Model to get the data. The Model creates Transfer Objects to send data back via Controller. The actual combo box is dynamically created using data from these objects.

Or does my controller call to the model to updateCombobox in the view?


The code which implements the Model should never communicate directly with a particular View. There should always be a Controller between a particular View and the Model. There is only one Model which may have multiple Views.

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Accidentally posted in a new thread:
Geoff Berl wrote:Thanks for the clarification, now that I think of it I'm not sure why I asked if the Model set the view's data. The intent behind MVC is to be able to reuse the model for any view which obviously wouldn't work if I programmed it to set component values in any particular view.

Yeah, the reason I say I've been working on understanding it for the past few days is because there are so many different variations, every time I tried to implement one, I would run into an issue and google it only to end up finding something somewhere that contradicted the original paradigm I was going with. I ended up figuring out that no one in particular was right but I chose to go with one where the model and view can only communicate via the controller, there is no path from view to model directly in either direction. To me this seems the easiest to start with, then I may venture into the ones that have the view call to the model for updates as I've seen in some implementations.

I do have one more question if you don't mind...

During the initial start up of the application I want my controller to populate the combobox in my view. Normally my controller get's the combobox component via the listener. How "should" my controller access this component when it isn't event driven? Currently I have created a method in the view like this

I didn't copy that directly so it may not be syntactically correct but you get the picture.

Is this a typical way to perform an action like this? I've also seen something like this
in which case the controller would get the JComboBox and work with it in the controller but that doesn't seem right.

In that case why wouldn't you just make jcbEmployees public, which also doesn't seem right?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
During the initial start up of the application I want my controller to populate the combobox in my view. Normally my controller get's the combobox component via the listener. How "should" my controller access this component when it isn't event driven?


Your implementation of a Controller is based on the Java Servlet Framework. There are various ways to incorporate functionality when an application is deployed in a Java web server. Keep in mind that when you first deploy the application, there is no active View at this point in time. What this means is that it impossible for the Controller to populate a combobox in a View that does not exist. So, you may want to think about this requirement again.

Ideally, you should not be trying to invent your own Controller code. A popluar Java-based framework already exists and contains a robust skeleton of Controller functionality. Check out the Apache Struts Framework for more information. Programmers customize a Struts-based Controller by creating specific Action classes and a set of XML-based configuration files.

If you want the application to extract data and create data objects when it is deployed, there are ways to do this via Struts ApplicationContext, Struts PlugIns, ServletContext, etc. This way when your View is created when there is a human user, the data objects will have already been created for the combobox creation process. Again, trying to implement your own Controller from scratch will present limitations otherwise already addressed by programmers of the past. The Struts Controller has been around for more than 10 years.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
If you are using Swing then look at working with the ComboboxModel to manipulate it's data: http://docs.oracle.com/javase/7/docs/api/javax/swing/ComboBoxModel.html
You would still only load the data when the combobox becomes visible so maybe look at when the panel/frame on which the combobox is placed is rendered.
 
Consider Paul's rocket mass heater.
 
subject: Quick question on MVC (DAO)