1

I've done project according to MVC pattern, now I'm looking to add API. I have a separate package for the myresource, and this package contains functions that should return data in JSON (just placeholder text for now). Now I am at the moment where I want to create function/method with the MySQL query and return requested data from MySQL DB. I want to create another method within my model file to keep MVC pattern. However, I do not know, for API should I crate this function in the API servlet or is it good to place this in the model and keep API servlet only to return requested data?

Luke_Nuke
  • 461
  • 2
  • 6
  • 23
  • 1
    from a design perspective it seems to me you're talking about a [DAO](https://it.wikipedia.org/wiki/Data_Access_Object); have alook also to the service which should use the DAo. See https://stackoverflow.com/questions/13785634/responsibilities-and-use-of-service-and-dao-layers – fantaghirocco Aug 27 '18 at 12:36
  • errata corrige: the previously was the italian wiki [DAO](https://en.wikipedia.org/wiki/Data_access_object) link – fantaghirocco Aug 27 '18 at 12:46

1 Answers1

1

It's a good idea to separate your sql queries from the api function. Also, I find it nicer that my API handlers are not polluted with ORM/SQL queries. An added benefit of this is that when you start adding unit tests to your project you can test your API handler independently of the model. i.e. for testing the api, mock the model function.

In the end, it's a matter of opinion and good practices.

psykeron
  • 796
  • 1
  • 6
  • 14