Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

database code in MVC model

 
Nev Mehta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65109
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All database information and activity should occur in the model. The controller should not be involved in anything to do with the DB.
 
Nev Mehta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65109
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic