Data Mapper, only for CRUD operations?

I have recently started reading about data mappers and all the articles I've found only demonstrate CRUD operations like:

$user = new User('John', 'Joe', 'john@hotmail.com');
$userMapper->insert($user);

// or this...
$user = $userMapper->fetchById(239);

Surely they are supposed to do more?

Currently in my application I use DAO's (or something similar anyway) so for example when I need a $user object one of my factories creates a $userDAO object and injects it into my $user object. And to do a query from my $user object I just do:

$this->userDAO->getNumActiveOrders($this->userId);

and it will do the query in the $userDAO object and return the result.

After loads of reading it seems my implementation is wrong because the domain object should not know about the DAO and vice versa. Am I right or wrong?

If it's wrong to do it that way then I assume that data mappers must be used for more than CRUD operations?

So if I wanted to find out how many active orders a user has I can do something like:

$userMapper->getNumActiveOrders($userId);

Would that be correct?

And if I wanted to set that value in my $user object I would have to do something like:

$user->setNumActiveOrders($userMapper->getNumActiveOrders($userId));

Using my implementation of DAO's seems to be a lot faster and uses less code than using data mappers but I am probably implementing data mappers wrongly.

Any advice would be great thanks.


After loads of reading it seems my implementation is wrong because the domain object should not know about the DAO and vice versa. Am I right or wrong?

That is correct.

If it's wrong to do it that way then I assume that data mappers must be used for more than CRUD operations?

The purpose of a DataMapper is to map data from a Database to Domain Objects. Since object graphs are usually not structured like data in a relational database system, you need some sort of mapper to get the relational data from the database into your objects and vice versa. DataMappers try to solve the problem of Impedance Mismatch.

So if I wanted to find out how many active orders a user has I can do something like:

$userMapper->getNumActiveOrders($userId);

Would that be correct?

Yes, you could it do it that way. But you could also query the user object for it, eg

echo $user->getActiveOrders();

and your user object would likely have some sort of Lazy Loading mechanism to fetch the Active Orders then.

And if I wanted to set that value in my $user object I would have to do something like:

$user->setNumActiveOrders($userMapper->getNumActiveOrders($userId));

No. You'd simply set the Active Orders. The number can be derived from them. If the count is something you want inserted in the database, you'd handle that in the Mapper.

Using my implementation of DAO's seems to be a lot faster and uses less code than using data mappers but I am probably implementing data mappers wrongly.

That's pretty much normal, because DAO's only query the database and don't do any mapping.

链接地址: http://www.djcxy.com/p/9618.html

上一篇: 数据映射器模式和关系数据

下一篇: 数据映射器仅适用于CRUD操作?