SheppardDigital
asked on
Understanding the Repository Pattern
Lately I've been reading up on the repository pattern, but I'm just not understanding it.
Currently, using my own simple framework (MVC) I have models, within each model I describe the fields available and create the necessary methods to be able to interact with the database. Each model inherits methods from a base function to perform actions such as 'listAll', 'save' and 'getById'.
I have a database class which handles the connection to the database via PDO, and binding of parameters. It has methods such as 'InsertObject','updateObje ct' and 'deleteObject' which accept an object and either insert, update or delete it from the database. This class also has some query building methods which allow me to build simple queries without writing the SQL. For example, a method in a model might look like this;
In my model, I might have other methods to manipulate the object. I suppose the class below isn't a great example as it is maybe best places in a helper class.
Now my questions about the repository pattern.
1) My model, it would just contain parameters for the object. Can it still include extra methods like the priceWithTax() method shown above? or other methods that manipulate the object. What if I wanted a method to return a users firstname and lastname together, can I still put that in my model?
2) The repository class, can you/would you put SQL queries directly in there if you didn't use a database class/library? I'm guessing if you use a database class, all interaction with the db class would happen in the repository?
3) In most projects, isn't this overkill? as I guess it's very unlikely that the datasource will change? If it did change, in my case I would re-write the models to use a different database class. Even if using the repository pattern, if the datasource changes, you'd still have to re-write or replace the repository classes?
Currently, using my own simple framework (MVC) I have models, within each model I describe the fields available and create the necessary methods to be able to interact with the database. Each model inherits methods from a base function to perform actions such as 'listAll', 'save' and 'getById'.
I have a database class which handles the connection to the database via PDO, and binding of parameters. It has methods such as 'InsertObject','updateObje
public function getByActiveStatus($active = 0) {
$db = new \Db\DbPDO();
$results = $db->select()->from('users')->where('active=:active',$isActive)->bind()->getRows();
return $this->arrayToObjects('user',$results);
}
In my model, I might have other methods to manipulate the object. I suppose the class below isn't a great example as it is maybe best places in a helper class.
public function priceWithTax() {
$tax = (($this->price) / 100) * $this->tax_rate;
return number_format($this->price + $tax,2);
}
Now my questions about the repository pattern.
1) My model, it would just contain parameters for the object. Can it still include extra methods like the priceWithTax() method shown above? or other methods that manipulate the object. What if I wanted a method to return a users firstname and lastname together, can I still put that in my model?
2) The repository class, can you/would you put SQL queries directly in there if you didn't use a database class/library? I'm guessing if you use a database class, all interaction with the db class would happen in the repository?
3) In most projects, isn't this overkill? as I guess it's very unlikely that the datasource will change? If it did change, in my case I would re-write the models to use a different database class. Even if using the repository pattern, if the datasource changes, you'd still have to re-write or replace the repository classes?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.