DDD以及Getter和Setter的使用 [英] DDD and the use of Getters and Setters

查看:129
本文介绍了DDD以及Getter和Setter的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了几篇有关Getter和Setter的用法以及它们如何帮助克服将域模型对象封装的目的的文章/帖子。我了解不使用设置器的逻辑-您允许客户端代码在对象业务规则和不变式的上下文之外操作该对象的属性。

I've read a few articles/posts regarding the use of Getters and Setters, and how they help to defeat the purpose of encapsulation in domain model objects. I understand the logic behind not using setters - you are allowing client code to manipulate attributes of that object, outside the context of the object business rules and invariants.

现在,此主体仍然让我感到困惑。例如,如果我需要更改对象的成员变量的值怎么办?例如,如果一个人的名字改变了,我该如何在模型中反映出来?一开始我想,为什么不拥有一个名为 ChangeName的函数,让我传递新名称,而该函数又可以更改内部的 name变量。嗯....那只是二传手而已!

Now this principal does still confuse me. For example, what happens if I need to change the value of a member variable of an object? For example, if the name of a person changes how can I reflect this in the model? At first I thought, well why not have a function called 'ChangeName' which let's me pass in the new name and it in turn can change the internal 'name' variable. Well.... that's just a setter isn't it!

我需要澄清的是-如果我要完全消除二传手,那么在上述情况下,我应该只依靠构造函数参数吗?我是否应该通过构造函数传递新的属性值代替旧的属性值,之后可以通过将对象传递给我拥有的任何持久性基础结构来保留更改?

What I need to clarify - if I were to completely eliminate setters, then in situations such as the above, am I supposed to solely rely on constructor parameters? Should I pass the new attribute value in place of the old attribute value via a constructor, after which I can persist the changes by passing the object to whatever persistence infrastructure I have?

这两篇文章在本次讨论中很有用:

These two articles are useful in this discussion:


  1. http://kellabyte.com/tag/ddd/

  2. http://typicalprogrammer.com/?p=23

  1. http://kellabyte.com/tag/ddd/
  2. http://typicalprogrammer.com/?p=23


推荐答案

嗯,这是经典的讨论。堆栈溢出中还有其他与此相关的线程。

Well, this is a classic discussion. There are several other threads here in Stack Overflow about this.

但是。获取/设置(自动属性?)并不都是不好的。但是它们往往使您将实体构造为仅包含prop而没有方法的死数据容器。这种迹象通常被称为贫血域-行为很少。我的建议是:

But. Get/Set's (Auto Properties?) are not all bad. But they tend to make you construct your entities as "dead" data containers that only have prop and not methods. The signs of this is often called Anemic Domain - and have very little behavior. My recommendation is:


  1. 尝试尽可能少地使用道具。

  2. 尝试查找属于前一组数据,应该像前一样是
    。名中间名和姓。另一个示例
    是邮政编码,城市,街道。最好通过
    方法设置这些数据。

  3. 通常可以将在一起的数据分组为Value
    对象。

  4. 更多的Value对象往往会从您的实体中带出更多描述性的方法,即动词,而不是通常的名词
    实体。

  5. 更多的方法值对象还可以增加更多
    的行为,并可能减少您的胖服务(也许您没有
    的服务中的业务逻辑泄漏太多...)。

  1. Try to use prop as little as you can.
  2. Try to find groups of data that belongs together and SHOULD be together like ex. Firstname Middlename and Lastname. Another example is Zipcode, City, Street. These data is better to set through a method. It minimizes the chances for your entity to be invalid.
  3. Often data that belongs together can be grouped as a Value object.
  4. More Value objects tend to bring out more descriptive methods from your entity that are "Verbs" instead of your usually "Nouns" entities.
  5. More methods for your value objects also opens up for adding more behavior and maybe reducing your "Fat" services (maybe you don't have services with too much leaked business logic...).

这里还有更多话要说...但是答案很短。
关于在构造函数中设置数据:仅当该实体在没有该数据的情况下无法存活 /存在时,我才这样做。对于实体Person,我会说Name也许不是那么重要。但是,社会安全号码可能是构造函数数据的候选者。或实体Employee必须在构造函数中具有Company,仅仅是因为雇员必须属于公司。

There are more to say here... but a short answer. About setting data in constructor: I only do that if this entity cannot "live"/exist without that data. For entity Person I would say that Name maybe isn't that kind of important. But Social Security Number may be a candidate for constructor data. Or entity Employee must have Company in constructor, simply because an employee must belongs to a company.

这篇关于DDD以及Getter和Setter的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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