This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Servlets and the fly likes database code in MVC model Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "database code in MVC model" Watch "database code in MVC model" New topic
Author

database code in MVC model

Nev Mehta
Greenhorn

Joined: Jan 09, 2007
Posts: 26
Where does the database code go in case of the MVC model? We could take the example SimpleCommand.war (http://www.coderanch.com/how-to/java/CodeBarnSimpleCommand) as a reference. So would the database connection go in one of the 'command classes'? If yes, then the database connection code would be part of the controller and not the model right?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

All database information and activity should occur in the model. The controller should not be involved in anything to do with the DB.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nev Mehta
Greenhorn

Joined: Jan 09, 2007
Posts: 26
So we would have some service objects (which are part of the model) which return populated Model objects to the Controller? Something like this.

So in that sense could we say that the UserService is also part of the model?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Yeah, if you are a fan of the service pattern, that's one way of doing it. Regardless, the point is that by keeping the presentation layer (controller/view) free from detailed information of how the model is getting/storing its data, the presentation layer stays decoupled from the details that only the model should have to deal with. This frees the model to do whatever it wants (use a database, use flat files, use stone knives and bearskins, use mock objects for testing, et cetera...)
 
GeeCON Prague 2014
 
subject: database code in MVC model