开发者

Domain Object that needs more than one Data Mapper

开发者 https://www.devze.com 2023-01-26 13:38 出处:网络
I\'m trying to figure out how to reuse Domain Models in different parts of the application and I have a feeling that the Data Mapper pattern is the way forward. The example below has methods that dire

I'm trying to figure out how to reuse Domain Models in different parts of the application and I have a feeling that the Data Mapper pattern is the way forward. The example below has methods that directly access the methods of the Mapper.

class Groups
{
    protected $_groups = array();

    public function addGroup($name)
    {
        $this->_groups[] = $name;
    }

    public function doSomethingGroupy($cakes)
    {
        // get all the groups that have cake
        return $cakeyGroups;
    }
}

... And a mapper to ma开发者_JAVA技巧tch the methods on the Groups class.

class GroupMapper
{
    public function find($id, Groups $group)
    {
         // Mappy type things, maybe some sql
    }

    public function fetchByNeediness($cuddles, Groups $group)
    {
         // More mappy type things
    }

    public function save(Groups $groups)
    {
         //  Saves
    }
}

However if sometime later I wanted to use the same Groups Models but populate the groups using different queries I would use a different mapper.

class AngryGroupMapper
{
    public function find($id, Groups $group)
    {
        // Something similar but with other tables and joins
    }

    public function fetchByRage($anger, Groups $group)
    {
        // Something new but only needed here
    }

    public function isEditable(Groups $groups)
    {
         // Do some querying
         return $bool;
    {
}

Now I Know the aim is Skinny Controller - Fat Model, so would I have another model to Map the Mapper (so to speak) to the Model?

class FatModelRepository
{
    public function getHappyGroups()
    {
        $mapper = new GroupMapper();
        return $mapper->fetchByNeediness('Puffy Shoes', new Groups());
    }

    public function getSadGroups()
    {
        $mapper = new AngryGroupMapper();
        return $mapper->fetchByRage('Aghh!', new Groups());
    {

    public function save(Groups $groups)
    {
        $mapper = new GroupMapper();
        return $mapper->save($groups);
    {
}


The Data Model should have no knowledge of the Data Mapper. Your Groups class/model shouldn't have find methods and it should not have access to the mapper.

Once you remove the mapper dependency from your model your problems will go away.

NOTE: check out Doctrine 2


As rojoca says you shouldnt have the fetch/find methods directly on the model. Technically hes also right about the model not storing a reference to the mapper, but in less complex situations i think this is ok so long as the model only excpets the most abstract form of mapper you plan on having (ie. some kind of base mapper class or an interface).

Given thos to things, you should only need to add methods to the mapper, and for this i would just use inheritance, ie. extend your groups mapper for the new functionality. Of course this requires that the mapper is injectable into the model. But if youre going to have the model hold a reference to its mapper then it does need to be injectable anyhow.

0

精彩评论

暂无评论...
验证码 换一张
取 消