Controller:
function indexAction()
{
if ($condition)
$this->renumPosition($id); //LINE #1
}
//Redirect only - function's sole purpose is to call another function
function renumPosition($id)
{
$this->repository->renumPosition($id);
}
Repository:
function renumPosition($id)
{
//SQL, DQL, ORM code follows
...
}
Question
Should Line #1 (above) be instead
$this->repository->renumPosition($id);
and should the renumPosition
within Controller be removed?
The question is .. when do I defer execution directly to my model/repository layer, and when do I call a Controller's function which defers to model/repository layer?
This perhaps is a really subtle question. Just to note, my intent here is NOT to save an extra function call, but to align myself with a programming philosophy.
For example, I can make an argument that when Controller should be cognizant of an action, I should have a public method
in Controller that may make a call to repository
. But then, when it is an internal action, maybe I can call repository directly without making a separate method call in Controller.
Does that make sense?
What my Goal/Concern is
Here is the Legacy code I started with:
Controller:
function indexAction()
{
if ($condition)
$this->renumPosition($id); //LINE #1
}
function renumPosition($id)
{
//Heavy SQL, DQL, ORM code follows - say 50-70 lines
...
$db->db_function($sql)
...
$db->db_function($moreSql)
//some more code here that relates to DB transactions
$db->db_function($moreSql)
}
As the code above was mostly storage related, I moved it all into my Repository
class. I ended up with this in my Controller
:
function renumPosition($id)
{
$this->repository->renumPosition($id);
}
I looked at the above and said to myself -- why do I have a function in my Controller that has a sole purpose of calling another function? Why have this indirection? Why have code cluttering my cognitive and screen space when I can totally and completely eliminate it. And so I did. I called the repository
function directly and removed Controller's "redirection" function entirely. But then I asked myself -- Did I do the right thing? That's what I am trying to figure out with my question.