域对象和值对象 - 它们相等吗?

发布于 2024-11-04 19:23:33 字数 2768 浏览 0 评论 0原文

通过查看 Zend 快速入门教程中的域对象示例以及考虑 DAO/VO 模式的其他示例,它们似乎非常相似。

我们能否推断出“值对象”与“域对象”是一样的?

如果不是,您能澄清一下它们之间的区别吗?

其中一个的功能是什么,如果另一个的功能又如何呢?

我问这个是因为,两者都是由 getter 和 setter 组成,仅此而已。看起来,它们执行相同的功能...

更新:

因此,Zend Framework 快速教程文档将其称为域对象:

 // application/models/Guestbook.php

    class Application_Model_Guestbook
    {
        protected $_comment;
        protected $_created;
        protected $_email;
        protected $_id;

        public function __construct(array $options = null)
        {
            if (is_array($options)) {
                $this->setOptions($options);
            }
        }

        public function __set($name, $value)
        {
            $method = 'set' . $name;
            if (('mapper' == $name) || !method_exists($this, $method)) {
                throw new Exception('Invalid guestbook property');
            }
            $this->$method($value);
        }

        public function __get($name)
        {
            $method = 'get' . $name;
            if (('mapper' == $name) || !method_exists($this, $method)) {
                throw new Exception('Invalid guestbook property');
            }
            return $this->$method();
        }

        public function setOptions(array $options)
        {
            $methods = get_class_methods($this);
            foreach ($options as $key => $value) {
                $method = 'set' . ucfirst($key);
                if (in_array($method, $methods)) {
                    $this->$method($value);
                }
            }
            return $this;
        }

        public function setComment($text)
        {
            $this->_comment = (string) $text;
            return $this;
        }

        public function getComment()
        {
            return $this->_comment;
        }

        public function setEmail($email)
        {
            $this->_email = (string) $email;
            return $this;
        }

        public function getEmail()
        {
            return $this->_email;
        }

        public function setCreated($ts)
        {
            $this->_created = $ts;
            return $this;
        }

        public function getCreated()
        {
            return $this->_created;
        }

        public function setId($id)
        {
            $this->_id = (int) $id;
            return $this;
        }

        public function getId()
        {
            return $this->_id;
        }
    }

1) 严格来说,我们是在面对“贫血域对象”?

2)它被称为“域对象”只是因为它包含域逻辑吗?

3) 如果是这种情况,那么,那些包含 findBookByAuthor(); 等方法的映射器他们也在处理领域逻辑,对吧?它们也可以被视为域对象吗?

多谢

By looking to the example of a Domain Object into Zend Quickstart tutorial, and other examples considering a DAO/VO patterns, they both seem to be very similar.

Can we deduce that to say "Value Object" is the same as to say "Domain Object" ?

If not, can you please clarify the differences between those?

What is the function of one, and what if the function of another ?

I'm asking this because, both are composed by getters and setters and nothing more then that. It seems that, they do the same function...

Update:

So, Zend Framework Quick Tutorial documentation called this, a domain object:

 // application/models/Guestbook.php

    class Application_Model_Guestbook
    {
        protected $_comment;
        protected $_created;
        protected $_email;
        protected $_id;

        public function __construct(array $options = null)
        {
            if (is_array($options)) {
                $this->setOptions($options);
            }
        }

        public function __set($name, $value)
        {
            $method = 'set' . $name;
            if (('mapper' == $name) || !method_exists($this, $method)) {
                throw new Exception('Invalid guestbook property');
            }
            $this->$method($value);
        }

        public function __get($name)
        {
            $method = 'get' . $name;
            if (('mapper' == $name) || !method_exists($this, $method)) {
                throw new Exception('Invalid guestbook property');
            }
            return $this->$method();
        }

        public function setOptions(array $options)
        {
            $methods = get_class_methods($this);
            foreach ($options as $key => $value) {
                $method = 'set' . ucfirst($key);
                if (in_array($method, $methods)) {
                    $this->$method($value);
                }
            }
            return $this;
        }

        public function setComment($text)
        {
            $this->_comment = (string) $text;
            return $this;
        }

        public function getComment()
        {
            return $this->_comment;
        }

        public function setEmail($email)
        {
            $this->_email = (string) $email;
            return $this;
        }

        public function getEmail()
        {
            return $this->_email;
        }

        public function setCreated($ts)
        {
            $this->_created = $ts;
            return $this;
        }

        public function getCreated()
        {
            return $this->_created;
        }

        public function setId($id)
        {
            $this->_id = (int) $id;
            return $this;
        }

        public function getId()
        {
            return $this->_id;
        }
    }

1) Strictly speaking, are we in face of a "Anemic Domain Object" ?

2) Is it called "domain object" just because it contains domain logic ?

3) If this is the case, then, those mappers containing methods like findBookByAuthor(); they are also dealing with domain logic right? Could they be considered domain objects as well ?

Thanks a lot

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

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

发布评论

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

评论(3

就此别过 2024-11-11 19:23:33

通常,值对象封装具有值的内容:货币、日期、温度等。它们可能包含值和单位,但它们并不复杂。

域对象可能更复杂(除非它是贫血域对象,它是一堆假装是域对象的 getter 和 setter),因为它包含域逻辑。

例如,您可能有一个发票域对象,其中包含许多发票行(每个发票项目一行),每个发票行可能有一个净额、一个税额和一个发票项目。金额和发票项目通常是值对象并且相当简单。

发票本身可能会很复杂,包括延迟付款的利率、审批流程的支持或会计系统的支持。

值对象足够简单,可以跨不同域重用。域对象对您的实际域进行建模,通常是为了对您的特定业务或域(包括您的业务逻辑)进行建模而编写的。

您经常会看到它们之间几乎没有区别的原因是许多开发人员将使用事务脚本/数据传输对象设计,但将其称为域模型。他们将 getter 和 setter 的集合标记为“域对象”。

Typically a value object encapsulates something that has a value: currency, dates, temperature, etc. They may contain a value and units, but they're not complex.

A domain object is likely to be more complex (unless it's an Anemic Domain Object, which is a bunch of getters and setters pretending to be a domain object) because it contains domain logic.

For example, you might have an Invoice domain object that contains many Invoice Lines (a line for each Invoice Item), and each Invoice Line might have a Net Amount, a Tax Amount, and an Invoice Item. The Amounts and maybe Invoice Item would typically be Value Objects and be reasonably simple.

The Invoice itself could be complicated with interest rates for late payment, support for an approval process, or support for your accounting system.

The Value Object is simple enough to be reusable across different domains. The Domain Objects model your actual domain and are typically written to model your specific business or domain, including your business logic.

The reason you'll often see little difference between them is that many developers will use a Transaction Script/Data Transfer Object design, but call it a Domain Model. They label their collections of getters and setters "domain objects".

九公里浅绿 2024-11-11 19:23:33

基于之前的回复,我认为它们相同:

  1. 域对象可以包含业务逻辑。它们代表问题空间中的实体,它们的属性值可以更改并由唯一的 ID 标识。
  2. 根据四人帮的说法,价值对象 是不可变的。这些对象不通过任何 ID 来标识,而是通过它们的值来标识。

Building on the previous responses, I am of the opinion that they are not the same:

  1. Domain objects can contain business logic. They represent entities in the problem space, their property values may be changed and are identified by a unique ID.
  2. According to the Gang of Four, Value Objects are immutable. Such objects are not identified by any ID, but instead by their value.
丢了幸福的猪 2024-11-11 19:23:33

它们可以是同一件事。在很多情况下确实如此。然而:

  • 领域对象可以执行业务逻辑(至少根据领域驱动设计),值对象不能
  • 拥有全部信息,而值对象只保存与其消费者相关的部分信息。

例如,对于 Invoice 域对象,它将与值对象相同,然后您可以为两者使用相同的类 - 它将包含发票编号、订购的商品、总价。

另一方面,User 域对象将具有密码字段和电子邮件字段,您希望能够在系统中处理这些字段,但绝不应将其发送到其他系统。因此,您需要一个新的值对象,它缺少这两个字段。

They can be the same thing. And in many cases they are. However:

  • domain objects can perform business logic (according to domain-driven design at least), value objects can't
  • domain objects have the whole information, while value objects hold only part of the information that is relevant to its consumer.

For example, in case of an Invoice domain object, it will be same as the value object, and then you can use the same class for both - it will have an invoice number, ordered items, total price.

On the other hand, a User domain object will have a password field and email field, which you want to be able to process within your system, but you should never send to other systems. Hence you need a new, value object, that lacks these two fields.

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