I am trying to separate out my code and have a logical question on the MC part. The model is supposed to be black-box to the controller correct? Right now I am using the controller to ask the model to retrieve information from the database. My model supports simple query building within itself for the database table is it linked to.
Let's say my model handles one database table and I need in my controller to search data in 5 different queries each with a simple where column = val. (this code is simple just for example/readability purposes) I could write it this way from the controller (I am using PHP):
Non-black-box
inside controller
$results1 = $table_model->getWhere(array('col1'=>'val1'));
$results2 = $table_model->getWhere(array('col2'=>'val2'));
$results3 = $table_model->getWhere(array('col3'=>'val3'));
$results4 = $table_model->getWhere(array('col4'=>'val4'));
$results5 = $table_model->getWhere(array('col5'=>'val5'));
However this now means the controller must know information about the database as in what the column name is. But I also do not need to write any additional functions within my model class to get the data I need so my model code is a lot simpler.
If I block-box it I have to write a function for each different request.
inside model
function getC1($val){/*sanitize $val code*/return $this->getWhere(array('col1'=>$val))}
function getC2($val){/*sanitize $val code*/return $this->getWhere(array('col2'=>$val))}
function getC3($val){/*sanitize $val code*/return $this->getWhere(array('col3'=>$val))}
function getC4($val){/*sanitize $val code*/return $this->getWhere(array('col4'=>$val))}
function getC5($val){/*sanitize $val code*/return $this->getWhere(array('col5'=>$val))}
inside controller
$results1 = $table_model->getC1('val1');
$results2 = $table_model->getC2('val2');
$results3 = $table_model->getC3('val3');
$results4 = $table_model->getC4('val4');
$results5 = $table_model->getC5('val5');
Now my controller doesn't need to know about the database but I had to add a lot of functions to get simple data that my controller can retrieve using my first method since the model has the capability to get it for simple queries.
Both methods work, I can see value to doing it each different way depending on how often the same results need to be retrieved in the application across different files. However, what is the best practice standard way in a simple case like this? I assume it is the black-box method and just having a lot larger model to handle all the similar but slightly different queries but I wanted to know what you think. Or should it be a mix where simple ones like the example would not use a separate function and save them for more complicated results but then you lose the whole black-box thing since you need to know the column name. I am in an environment where I am the only developer and I am writing all the code and manage the database/names if that makes a difference? (but I don't think it should make a difference?)
Thanks for your input.
Aucun commentaire:
Enregistrer un commentaire