aspose file tools*
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: 61648
    
  67

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: 61648
    
  67

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...)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: database code in MVC model