Jump to content

Best Practice


utexas_pjm

Recommended Posts

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

Link to comment
Share on other sites

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?

3tier.png

 

Traditional MVC HTTP request:

mvcwidgets.png

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.