需要多个数据映射器的域对象

发布于 2024-10-03 06:01:02 字数 1742 浏览 3 评论 0原文

我正在尝试弄清楚如何在应用程序的不同部分重用域模型,并且我感觉数据映射器模式是前进的方向。下面的示例具有直接访问映射器方法的方法。

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;
    }
}

...还有一个映射器来匹配 Groups 类上的方法。

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
    }
}

但是,如果稍后我想使用相同的组模型但使用不同的查询填充组,我将使用不同的映射器。

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;
    {
}

现在我知道目标是瘦控制器 - 胖模型,那么我是否有另一个模型将映射器(可以这么说)映射到模型?

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);
    {
}

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 match 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);
    {
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拒绝两难 2024-10-10 06:01:02

数据模型不应该了解数据映射器。您的 Groups 类/模型不应具有查找方法,也不应访问映射器。

一旦从模型中删除映射器依赖项,您的问题就会消失。

注意:查看Doctrine 2

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

盛装女皇 2024-10-10 06:01:02

正如 rojoca 所说,您不应该直接在模型上使用 fetch/find 方法。从技术上讲,他关于模型不存储对映射器的引用的说法也是正确的,但在不太复杂的情况下,我认为只要模型仅包含您计划拥有的映射器的最抽象形式(即某种基本映射器),我认为这是可以的类或接口)。

鉴于此,您应该只需要向映射器添加方法,为此我将只使用继承,即。扩展您的组映射器以获得新功能。当然,这要求映射器可注入模型中。但是,如果您想让模型保存对其映射器的引用,那么无论如何它都需要可注入。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文