在域模型对象中使用工厂?

发布于 2024-08-18 16:47:46 字数 913 浏览 5 评论 0原文

场景:

在我的应用程序(使用丰富的域模型,其中逻辑位于模型中,而不是在服务中)中,我有用户。我使用服务创建新用户

User newUser = userService.createNewUser("Hans Dampf");

或从数据库获取它们

User oldUser = userDao.findByName("Hans Dampf");

因为在每次调用我的应用程序时我都可以直接访问用户对象,所以我想使用用户对象作为我的域模型的入口点。

每个用户可以有不同类型的画廊,保存在另一个表中。

class User {
 @OneToMany(fetch = FetchType.LAZY)
 @JoinColumn(name = "userId")
 private Set<Gallery> automatic = new HashSet<Gallery>();
}

我想要一种简单的方法来启用特定的画廊。所以我的 API 看起来像:

User user = ... // creating or retriving user
user.enableMainGallery();

在这个方法中,有必要创建一个新的画廊对象并将其添加到画廊列表中。但是如何创建这个新实例呢?使用工厂?这需要将工厂注入域对象(可能有问题)。

public void enableAutomaticGallery() {
    automatic.add(automaticFactory.createAutomaticGallery(this));
}

或者我的接口定义有缺陷?我应该以其他方式定义它,这样我就不必注入工厂吗?如何?

Scenario:

In my application (which utilises an rich domain model, where the logic is in the model, not in the services) I have users. I create new users with a service

User newUser = userService.createNewUser("Hans Dampf");

or get them from the database

User oldUser = userDao.findByName("Hans Dampf");

Because in every call into my application I have direct access to an user object, I would like to use the user object as an entry point into my domain model.

Each user can have different types of Galleries, saved in an other table.

class User {
 @OneToMany(fetch = FetchType.LAZY)
 @JoinColumn(name = "userId")
 private Set<Gallery> automatic = new HashSet<Gallery>();
}

I want to have a simple way to enable a specific gallery. So my API would look like:

User user = ... // creating or retriving user
user.enableMainGallery();

Inside this method it would be necassary to create a new gallery object and add it to the list of galleries. But how to create this new instance? Using a factory? That would require to inject the factory into the domain object (may be problematic).

public void enableAutomaticGallery() {
    automatic.add(automaticFactory.createAutomaticGallery(this));
}

Or is my interface definition flawed? Should I define it in some other way, such that I don't have to inject the factory? How?

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

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

发布评论

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

评论(2

反差帅 2024-08-25 16:47:46

正如您所说,域对象不应该依赖于应用程序级对象,例如工厂或 Daos。

该域通常是:

  • 足够复杂,满足功能需求,而不添加其他问题(例如持久性、验证、GUI 等...)
  • 中心并在任何地方使用(因此它的复杂性会损害您在许多编码活动中的生产力)
  • 在相关应用程序和所有层中可重用,甚至可以序列化并发送到客户端或 WebService 上的不同 JVM(除非它依赖于应用程序) -级别对象)

因此您的 enableAutomaticGallery 方法应该位于 Service 对象上。
它可以具有相同的代码,但取决于应用程序。

As you say, the domain objects should not be made dependent on application-level objects such as factories or Daos.

The domain is typically :

  • complex-enough with the functional needs, without adding other concerns (such as persistence, validation, GUI etc...)
  • central and used everywhere (so it's complexity damages your productivity in many coding activities)
  • reusable across related applications, and in all layers, can even be serialized and send to a different JVM on a client or a WebService (unless it has dependencies on application-level objects)

So your enableAutomaticGallery method should be on a Service object.
It can have the same code, but will be application-dependent.

为人所爱 2024-08-25 16:47:46

您的域模型应该对任何服务或 DAO 层甚至任何工厂对象一无所知。

我建议您不要使用需要将 Gallery 对象添加到实例集合的方法 User.enableMainGallery() ,而是公开一个方法User.addGallery(图库)

这样,负责“启用图库”的类通过将对象添加到 list 集合中来实现此目的。

换句话说,我不认为 User 对象应该负责了解“启用主图库”的含义。这听起来像是属于业务逻辑范畴的东西(我知道这是一个含糊的术语)。

Your domain model should know nothing about any service or DAO layers or even any factory objects.

I would suggest instead of a method User.enableMainGallery() which needs to add Gallery objects to the instance collection (as you've said), that you instead expose a method User.addGallery(Gallery).

This way, the classes responsible for "enabling the gallery" do so by adding objects to the list collection.

In other words, I don't believe that the User object should be responsible for knowing what it means to "enable a main gallery". This sounds like something that falls under the umbrealla of business logic (ambiguous term, I know).

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