域对象和值对象-它们相等吗? [英] Domain Objects and Value Objects - are they equal?

查看:106
本文介绍了域对象和值对象-它们相等吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过在Zend Quickstart教程中查看域对象"示例以及其他考虑DAO/VO模式的示例,它们似乎非常相似.

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 ?

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

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

更新:

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

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)严格来说,我们面对的是贫血领域对象"吗?

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

2),因为它包含域逻辑,所以它被称为域对象"吗?

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

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

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 ?

非常感谢

推荐答案

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

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".

这篇关于域对象和值对象-它们相等吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆