php - Design patterns for data source independent model? -
say want build application in models have different data sources (like mysql, api, xml, etc).
what common approache(s) in order implement this, , design patterns used?
i think dao looking for.
think like:
interface rdbmsdriver { public function connect(); public function disconnect(); public function query($sql); public function fetchall($sql); } class mysqlidriver implements rdbmsdriver { public function connect() { } public function disconnect() { } public function query($sql) { } public function fetchall($sql) { } } class pgsqldriver implements rdbmsdriver { public function connect() { } public function disconnect() { } public function query($sql) { } public function fetchall($sql) { } } abstract class rdbmsdao { protected $driver; public function __construct(rdbmsdriver $driver) { $this->driver = $driver; } } interface someentitydao { public function insert(someentity entity); public function update(someentity entity); public function delete(someentity entity); public function find($entitykey); public function findall(); } /** * data relational databases. */ class someentityrdbmsdao extends rdbmsdao implements someentitydao { public function insert(someentity entity) { } public function update(someentity entity) { } public function delete(someentity entity) { } public function find($entitykey) { } public function findall() { } } // use like: new someentityrdbmsdao(new mysqlidriver(...)) /** * data webservice */ class someentitywebservicedao implements someentitydao { public function insert(someentity entity) { } public function update(someentity entity) { } public function delete(someentity entity) { } public function find($entitykey) { } public function findall() { } } class someentitymodel { private $persistance; public function __construct(someentitydao $persistance) { $this->persistance = $persistance; } }
Comments
Post a Comment