I have a question regarding database access in a mvc application. Where should my database access logic placed?
should it be placed in each model? (if I have a Person model)
Person p = new Person();
p.save();
should it be placed in each controller? or should I create a different set of classes only to perform the database logic, which means I have an additional layer in addition to model,view and controller?
How is this done? Also what if an ORM is used?
In MVC pattern, M means models, V means view, C means controller. A common MVC application progress is that once a request is coming, controller get it and do necessary processing, retrieving results data, then pass the results data to view for rendering. After view layer is rendered, it displays to users via GUI.
Controller can be regarded as a commander, it controls process, but it is not good to handle data retrieving in controller. Model should be responsible for retrieving and organizing data. That means data objects should be stored in Model instead of Controller, Controller calls Model to retrieve data objects.
For your case, my suggestion is it needs following components:
PersonController
, it calls PersonService.savePerson(Person person)
method to save data(or in other case it retrieves result). I suggest Controller layer should be thin.PersonService
, it has method savePerson(Person person)
, this method calls PersonDAO.savePerson(Person person)
method to save/retrieve projects data(here it saves data), and maybe other handling. Here business logic goes.PersonDAO
, it has several methods which deal with Person
objects(like savePerson(Person person)
, getAllPersons()
), deal with database in this layer. But these methods should be independent with business logic(as business logic should be deal in PersonService
).Person
object, it is value object, it just defines what attributes a Person
should have, like name
, age
, etc, with get/set
methods, used for passing data through different layers. It does not deal with database at all.While for uncomplex application, Service layer is not very necessary and can be integrated to Controller layer, which means just PersonController
is ok.
Others have pointed out that database access is a Controller function, but I recommend that you create a new library project to handle the database interaction. Typically this will have a name like "customerService.jar" - i.e. it encapsulates everythng you want to do to a customer. This will simplify your application, and add greatly to the reusability of your code.
This would be by approach:
So if your needs change in future (e.g. You need to create an IOS or Android native app) you have a re-usable service that will happily do the job. Similarly if the architects insist on exposing it as a REST or SOAP service, you only need add the "glue logic" to implement the REST or SOAP service.
Also, it is easy to mock out the customerService in web unit tests.
Best of luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With