MVC Database Persistence – Where Should Database Persistence Code Sit in an MVC System?

mvc

I've seen multiple configurations for persisting information to the database. Generally, three types of designs seem common in my corner of the world:

  • Controller manages the persistence
  • Model manages the persistence
  • Third party library manages the persistence, usually requiring some sort of annotations on the model.

I'm wondering which configuration (if any) is, conceptually, the easiest to use/most compatible with an MVC architecture?

(If it's not one I listed, please give a quick outline/overview as part of the answer)

Best Answer

Your second and third options are identical. The M in MVC is not the data model, but rather the domain model. This includes persistence, whether done directly or via an ORM, and both are perfectly correct.

The controller should manage the flow of the site and passes stuff off to the domain (sometimes via a service layer) to be handled, so persisting from there is wrong -- or at least semantically uncomfortable.