在域模型对象中使用工厂?
场景:
在我的应用程序(使用丰富的域模型,其中逻辑位于模型中,而不是在服务中)中,我有用户。我使用服务创建新用户
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所说,域对象不应该依赖于应用程序级对象,例如工厂或 Daos。
该域通常是:
因此您的
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 :
So your
enableAutomaticGallery
method should be on a Service object.It can have the same code, but will be application-dependent.
您的域模型应该对任何服务或 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 addGallery
objects to the instance collection (as you've said), that you instead expose a methodUser.addGallery(Gallery)
.This way, the classes responsible for "enabling the gallery" do so by adding objects to the
listcollection.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).