无需 Getter 即可生成变更日志

发布于 2025-01-10 20:48:09 字数 424 浏览 3 评论 0原文

通过互联网阅读,我看到很多人说 Getter/Setter 是邪恶的。您需要将数据隐藏为私有,仅提供函数并使用对象之间的消息来完成工作。我尝试在代码中对 Setter 执行此操作,并取得了一些成功,但是当涉及到 Getters 时,总是存在不可能的情况:类中的数据可以'不会永远留在记忆里,你需要一直坚持下去。

一种经常出现的情况是:当你尝试更新一个实体并需要生成更改日志时,你需要比较两个对象,如果没有 Getter,你如何完成这一点?

与持久化到数据库相同(也许我可以用它来生成 PO 对象)

编辑:

也许我可以这样构建它:在 DDD 中,我希望我的实体具有尽可能少的 Getters,但是在基础设施层,需要持久化它,我必须公开所有数据

Reading through the internet, I see a lot of people saying Getter/Setter is evil. you need to hide the data as private and only provide functions and use messages between objects to do work. I try to do this to Setters in my code with some success, but when it comes to Getters there are always cases this is not possible: the data within the class can't be in the memory forever, you always need to persistent it.

One case that comes quite often is: when you try to update an entity and need to generate a change log, you need to compare the two objects, without Getters, how can you accomplish this?

Same with persistent into the database(maybe I can have it to generate a PO object)

Edit:

Maybe I can frame it like this: in DDD I want my Entity to have as few Getters as possible, but in the infrastructure layer, in need to persistent it , I have to expose all the data

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

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

发布评论

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

评论(1

对你的占有欲 2025-01-17 20:48:09

我认为我没有正确理解您想要实现的目标,但我会尝试详细说明一些我认为可能有帮助的事情。

Getters 和 Setters 绝不是邪恶的;相反,它们是Java的基本概念。并非类的所有字段都必须是私有的。这是根据您试图用代码表达的内容以及应如何使用该类的实例而做出的决定。您应该避免编写除获取或设置值之外执行任何其他操作的 Getters 和 Setters。

话虽这么说,在大多数情况下,最好使用私有成员变量并编写 Getters(有时还包括 Setters)来访问该类实例中的数据。将类的实例想象为数据的胶囊,其中捆绑了特定上下文中所需的信息。问问自己,为什么来自另一个上下文的任何其他对象都应该直接访问该实例中的数据 - 甚至操纵它?理想情况下,实例数据的操作应该由实例本身通过类提供的方法来完成。为什么?只有实例本身知道其上下文,并且只有实例本身确切地知道其字段在该上下文中表达的目的。按照这种思路,私有字段有助于设计更简洁、更具表现力的代码。它们有助于跟踪您的课程的目的是什么。您应该始终有意识地决定哪些数据应该被读取(getter),如果需要,哪些数据应该从外部直接写入(setter)。

我不知道你的变更日志到底应该包含什么。如果您使用数据库,那么您很有可能可以访问可以帮助您跟踪更改的审核框架。否则,我建议对相关类使用面向方面的方法。请参阅AspectJ 的简短介绍来了解这个概念。

I think I do not understand correctly what you are trying to achieve, but I will try to elaborate on a few things that I assume that could help.

Getters and Setters are in no way evil; on the contrary, they are fundamental concepts of Java. Not all fields of a class have to be private. It is a decision based on what you are trying to express with your code and how instances of that class should be used. You should refrain to write Getters and Setters that do anything else but just get or set a value.

That being said, in most cases it is good practice to use private member variables and write Getters (and sometimes Setters) to access the data within an instances of that class. Imagine an instance of a class as a capsule for data that bundles information that you need in a certain context. Ask yourself, why should any other Object from another context have direct access to the data within that instance - or even manipulate it? Manipulation of the instance's data should ideally be done by the instance itself, through methods provided by the class. Why? Only the instance itself knows its context and only the instance itself knows exactly which purpose its fields express in that context. In this line of thinking private fields help to design cleaner and more expressive code. They help keeping track of what your classes' purpose is. You should always consciously decide which data should be read (getter) and, if needed, which data should be directly written (setter) from outside.

I don't know what exactly your changelog should contain. If you are using a database, there is a good chance you might have access to an auditing framework that could help you with tracking changes. Otherwise I would recommend to use an Aspect Oriented approach for the classes in question. See this short introduction for AspectJ to get an idea of the concept.

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