How do I use DataMapper Pattern? I can use it as a glorified Database Access Object, aka
TableGateway (loading a single class from a single database table), or I can use it more to drive business concerns (i.e. load complex objects from many DB tables). Or mix both?
Suppose that in my business application I have a
Product and a
Motor entities. Sometimes I need just one and sometimes just the other. But sometimes I need a structure where a motor belongs to a product. (Product owns the Motor), and I need to load both, and I can create and use a single function of my DataMapper to do that.
$products = new ProductDataMapper(); //This is the business-relevant construct, loading a complex object $product = $products->getProductWithMotor(1, 2);//ids echo $product->getName(); //"Product with id 1" echo $product->getMotor()->getName(); //"Motor with id 2"
$products = new ProductDataMapper(); //just loading an object by id, no specific business meaning $product = $products->findById(1); $motors = new MotorDataMapper(); //just loading an object by id $motor = $motors->getMotor(2); //business-relevant line (Product owns the Motor) //that was hidden away inside the getProductWithMotor above, //now inside some business layer $product->setMotor($motor); echo $product->getName(); //"Product with id 1" echo $product->getMotor()->getName(); //"Motor with id 2"
My question deals with "What was/is the intent of use of DataMapper pattern?"
Is there intent to use as a business layer (namely load business-relevant objects with
DataMapper functions creating complex business-relevant objects), or is it purely intended to do simple Create/Update/Delete/Insert functions and is to disconnect itself from creating business-relevant-objects, leaving that to some business functionality layer?