dianovich
dianovich

Reputation: 2287

Model logic and service layer logic

What I've found recently is that I'm coding across multiple layers of an application to achieve something very simple.

For example

$domain = new Application_Model_Domain(); 
$table = $domain->getMapper()->getDbTable();
$row = $table->find($id)->current();
$row->delete();

This could all easily be done in a delete() method in my service layer. However, I've been moving this functionality into a model mapper:

# service layer
$domain = new Application_Model_Domain();
$domain->getMapper()->delete($id);

# mapper
function delete($id) {
  $table = $this->getDbTable();
  $row = $table->find($id)->current();
  $row->delete();  
}

Which approach would you consider most valid?

Upvotes: 4

Views: 477

Answers (3)

Nazar Merza
Nazar Merza

Reputation: 3454

In general it is better to have a separate layer for data access. It is a well know pattern, called Data Access Layer (DAO). Because, if you think about it, data access doesn't really belong to either of model or service layers. Service layer can then, use DAO layer.

Upvotes: 0

AlexTheo
AlexTheo

Reputation: 4164

In my opinion all the database related operation must be in the model layer. So you will be able to replace your logic layer without rewriting the same code just by using the existing model layer.

Upvotes: 0

Jeremy Harris
Jeremy Harris

Reputation: 24549

Having the function contained within your model mapper helps you avoid repeating code in multiple places of your application (i.e. enforces DRY principles) as well as makes debugging and code modifications a lot easier.

Upvotes: 1

Related Questions