utexas_pjm Posted April 3, 2007 Share Posted April 3, 2007 Hey guys, I have a question about "lazy loading" and 3 tier architecture. I have been debating with myself over the past few days the best way to implement "lazy loading" while not breaking any object oriented paradigms. The most intuitive approach, in my mind, is one like the following: <?php /** * * Hypothetical Widget Class. * * Hypothetical Widgets are * composed of a name and an array * of other Widgets. * */ class Widget { var $name; var $widgets; function Widget($name, $widgets = array()) { $this->name = $name; $this->widgets = $widgets; } function doSomething() { // TODO } } /** * * Hypothetical Widget Data Access Object * */ class WidgetDAO extends DAO { function getAllWidgets($dataSourceConnection) { $rs = SomeDBAbstractionClass::executeQuery( GET_ALL_WIDGETS_SQL, $dataSourceConnection); if($rs->encounteredError()){ WidgetDAO::_throwError($rs->getErrorMessage()); return null; } $widgets = array(); foreach($rs as $row){ // Because of the recursive nature of // widgets, as I've defined them, there is alot of // work to be done in the buildWidgetFromRow method. // I'd prefer to run this little as possible. $widgets[] = WidgetFactory::buildWidgetFromRow($row); } return $widgets; } } /** * * Hypothetical Controller * */ class SomeController extends AbstractController { var $widgets; function _loadAllWidgets($lazyLoad = false) { // If $lazyLoad is true and there is something in the $widgets array just return it. // Otherwise populate the array from the DAO. $this->widgets = (isset($this->widgets) && $lazyLoad) ? $this->widgets : WidgetDAO::getAllWidgets( DBConnectionFactory::checkoutMySQLConnection()); return $this->widgets; } } ?> While this approach works, I feel like I am blurring the line between the model(WidgetDAO) and controller(SomeController) layers. Should I instead move the lazy load logic into the DAO and force any call from the service layer to specify whether it wants fresh or cached data, then store these arrays in external registries? I'd appreciate any insight. Best, Patrick Quote Link to comment https://forums.phpfreaks.com/topic/45463-best-practice/ Share on other sites More sharing options...
448191 Posted April 4, 2007 Share Posted April 4, 2007 I do not know much about implementing 3 tier architecture, but the intention you are describing sounds more like the MVC. From what I know about 3 tier architecture, there is no clear seperation between control- and business logic (logic tier). DAOs are part of the data tier though. n Tier and MVC are conceptually different though. It should be possible to apply an MVC architecture within a n Tier architecture. To make a long story short: from an MVC standpoint, yes you are fading the lines between the Model and Controller. I do not think you are breaking with the 3 tier paradigm though. To illustrate: Assumes active presentation tier: Ajax? Traditional MVC HTTP request: Quote Link to comment https://forums.phpfreaks.com/topic/45463-best-practice/#findComment-220969 Share on other sites More sharing options...
utexas_pjm Posted April 4, 2007 Author Share Posted April 4, 2007 Thank you for the response. I consider MVC implementations to be a subset of 3-tier architecture, and I tend to use these interchangably although I should probably try to be be more precise. With regard to the MVC pattern where should I hold the pre loaded widgets? Surely not in the DAO, perhaps a service layer between the DAO and the Controller, like the following: <?php class EntityRegistry { var $widgets; function loadWidgets() { $this->widgets = WidgetDAO::getAllWidgets(DBConnectionFactory::checkoutMySQLConnection()); return $this->widgets; } function lazyLoadWidgets() { // If there is something in the $widgets array just return it. // Otherwise populate the array from the DAO. $this->widgets = (isset($this->widgets)) ? $this->widgets : WidgetDAO::getAllWidgets(DBConnectionFactory::checkoutMySQLConnection()); return $this->widgets; } } ?> Thanks again for the insight, Patrick Quote Link to comment https://forums.phpfreaks.com/topic/45463-best-practice/#findComment-221105 Share on other sites More sharing options...
448191 Posted April 4, 2007 Share Posted April 4, 2007 How about GoFs Proxy, or one of the other solutions suggested in PoEAAs 'lazy load' chapter? Quote Link to comment https://forums.phpfreaks.com/topic/45463-best-practice/#findComment-221415 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.