域对象行为如何

发布于 2024-11-30 03:09:32 字数 298 浏览 0 评论 0原文

这是一个来自新手的相当愚蠢但完全有效的问题。 上下文:我的“域对象”有一个名称(和一些其他属性,但它们并不重要)。该名称是根据数据库中定义的一些模式生成的。 现在...谁应该确定域对象的名称?域对象是否应该确定它自己的名称(请注意,这个“名称”可以基于它自己或其他对象的属性),或者服务应该根据模式确定名称,然后调用类似

domainObject.setName(theActualName)

我倾向于遵循“服务”的名称”方法,但另一方面,这项任务似乎应该是特定于对象的行为。

非常感谢您的回复。

Here's a rather dumb but perfectly valid question coming from a newbie.
Context: My "domain object" has a name (and some other attributes, but they are not important). This name is generated according to some patterns which are defined in the database.
Now... who should determine the name of the domain object? Should the domain object determine it's own name (please note this "name" can be based on it's own or some other object's attributes) or should a service determine the name according to the pattern and then call something like

domainObject.setName(theActualName)

I tend to follow the "service" approach, but on the other hand it seems this task should be an object-specific behaviour.

Thanks a lot for your replies.

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

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

发布评论

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

评论(3

只涨不跌 2024-12-07 03:09:32

我认为你应该看看告诉不要问原则

如果对象包含它的所有数据需要计算它的名称我会将此方法放在该类上。如果这个数据是基于其他对象的属性并且父类也包含这个包含数据的子对象,我会再次放在父类上。

保持简单即可。如果我真的不需要,我不会引入服务类。用 DDD 术语来说,您应该将域相关逻辑放在域类中。

I think you should look at Tell Don't Ask Principle

If object contains all the data it needs to calculate its name I would put this method on that class. If this data is based on other object's attributes and parent class also contains this child object that contains data, I would again put on the parent class.

Just Keep it simple. I wouldn't introduce a service class if I dont really need. In DDD terms you should put your domain related logic in domain classes .

瞎闹 2024-12-07 03:09:32

这是一个包含逻辑的领域对象?如果是的话,我宁愿给它一个 DAO 的引用,并让它自己决定它的名字。这样你的逻辑就会接近你的数据。

This is a domain object that contains logic? If yes, I'd rather give it a reference to DAO and let it decide its name on its own. In this way your logic will be near your data.

遗弃M 2024-12-07 03:09:32

有几个问题与此相关:

  1. 您是否需要对名称进行某种管理? (一个示例是按名称查找对象的目录。)如果是这样,请考虑使用服务。 (另一种方法是安排每个域对象在确定其名称后进行自我注册。)
  2. 一旦创建并命名域对象,它们在逻辑上是否独立于数据库?如果是这样,那么通过将数据库依赖项放入服务中来保持它们的独立性将是一个很好的做法。

如果域对象无论如何都要与数据库绑定,并且不需要集中管理名称,那么我认为拥有域对象名称本身会带来更大的 对象内聚

A couple of issues bear on this:

  1. Do you need some sort of management of the names? (An example would be a directory to look up an object by name.) If so, consider using a service. (An alternative would be to arrange for each domain object to register itself once it figures out its name.)
  2. Are the domain objects logically independent of the data base once they have been created and names? If so, then it would be good practice to keep them independent by putting the data base dependence in a service.

If the domain objects are going to be tied to the data base anyway, and if there is no need to centrally manage the names, then I would think that having the domain objects name themselves leads toward greater object cohesion.

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