0

Seeking some best practice advice. Basically I have a large entity that consists of various properties where some properties are other entities.

What is the best practice of "instantiating" the big entity in my datamapper? Do I call the other mappers for the other entities inside my "main" mapper? Example, I have an entity "Big" which consists of various properties where the "customer" property is a customer entity, is this the best way todo it?

class BigMapper{

  //Find and return a BigEntity
  function find($id){
    $customerMapper = new CustomerMapper($this->db);
    $customer = $customerMapper->findByBigEntityId($id);
    $bigRow = $this->db->fetchRow('SELECT * FROM big WHERE id = ?', $id);
    $bigRow['customer'] = $customer; //This line feels sort of ugly.... and then:
    return( new EntityBig($bigRow) );
  }

}

I have seen solutions where people instantiate the other mappers in the service layer and then sort of validate each entity by itself, and finally combine them all etc, but I'm really not sure what is the best way of "building" a large entity based on several other entities.

Any comment or help appreciated, thanks!

neph
  • 733
  • 1
  • 8
  • 16

1 Answers1

1

If you have all properties in the data layer at once, its a good idea to map them all. It would make the big entity complete in itself.

But if you need to do Lazy loading. You don't need all data at once(i.e customer entity), you can just map the ID of the customer entity first. And if later needed you can make a call to database to fetch customer entity on the basis of its ID and then map the Customer entity in the Big Entity.

Vaibhav Jain
  • 33,887
  • 46
  • 110
  • 163