Responsibility of data persistence in the MVC pattern

In our experimental application the initial MVC pattern assumed the Model would not only be responsible for managing data within the application but also for persisting it client-side using the HTML5 Local Storage facility (Sprint 2b). As we introduce the AJAX/AJAJ mechanism for interacting with the web server, in order to persist the data via a database server, a couple of questions arise regarding the MVC pattern.

“Is the Model the most appropriate component to manage persistence or should the Controller perform that role?”

“If the Controller should manage persistency, is this only appropriate when an external database is used or should it be the same whatever the storage facility?”

There are a number of options:

  1. Relocate the persistence functionality to the Controller before employing AJAX. Then we can substitute Local storage for remote (database) storage or even employ both, just in case we loose communications.
  2. Retain the functionality within the Model but let the Controller decide when/if it is to be used. I.e. in stead of the Model automatically storing data whenever it is changed, it notifies the Controller of the need to preserve the data. The Controller can then decide if the Model should store the data in Local Storage or if it should send the data back to the database, or both.
  3. Finally, all the persistency code should be within the Model but this means potentially including business logic in the Model to control how/when it is to be performed; and business logic is the responsibility of the Controller not the Model.

Hopefully you see the dilemma, but I suspect there is no one answer.

Return to the previous article.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s