使用 Morphia 持久化域对象而不向域对象添加不必要的注释的策略?
假设,假设我有一个名为 Person
的域对象。它看起来像这样:
public class Member {
private final String firstName;
private final String lastName;
private final String email;
private final String password;
public Member(String firstName, String lastName, String email, String password) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
}
我还有一个 MemberRepository 接口,它定义了基本的 CRUD 和其他一些糖方法。
现在假设我想使用 Morphia 将这个域对象保留在 MongoDB 实例中。我已经创建了 MorphiaMemberRepository 实现,但我不确定如何以尽可能少的混乱来存储域对象。
任何 Morphia 用户都会知道我需要创建一个 ObjectId
类型的 ID 字段并用 @Id
对其进行注释。此外,我需要用 @Entity("members")
注释该类。我不一定想用 Morphia/MongoDB 特定的注释来弄乱我漂亮的域对象。
那么...各位堆栈人员,我应该怎样做才能使这个实现尽可能干净?
Hypothetically, lets say I have a domain object called Person
. It looks like such:
public class Member {
private final String firstName;
private final String lastName;
private final String email;
private final String password;
public Member(String firstName, String lastName, String email, String password) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
}
I also have a MemberRepository interface that defines basic CRUD and some other sugary methods.
Now lets say I want to persist this domain object in a MongoDB instance using Morphia. I've created my MorphiaMemberRepository implementation but what I'm unsure of is how to store the domain object with as little mess as possible.
Any Morphia users would know that I'd need to create an ID field of type ObjectId
and annotate it with @Id
. Additionally I'd need to annotate the class with @Entity("members")
. I don't necessarily want to clutter up my nice domain object with the Morphia/MongoDB specific annotations.
So...fellow stackers, what should I do to keep this implementation as clean as possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 Morphia 的要求(至少是 @Id 的要求)。注释不需要改变您使用对象或序列化的方式。它们只是大多数程序忽略的额外元数据;它们是无害的。
如果您有一个唯一的字段,那么您不需要添加任何新字段,只需使用 @Id 标记该字段即可完成。
如果您确实不想执行任何操作,您可以在吗啡中手动创建元数据来处理您的类,但这将需要更多工作,因为该过程不会通过任何外部配置格式公开。
That is the requirement for Morphia (at least the @Id one). Annotations do not require changing the way you use your object or serialization. They are just extra metadata which most programs ignore; they are harmless.
If you have a unique field then you don't need to add any new ones, just mark that with @Id and be done with it.
If you really don't want to do any of this, you can manually create the metadata in morphia to deal with your classes, but that will be much more work as that process is not exposed via any external configuration format.
假设有 IMember,那么
Member 实现了 IMember
。 Getter 方法在 IMember 中定义。另一个类
MorphiaMember 实现了 IMember
根据需要进行了注释,并且具有 ID 字段(id 并不总是 ObjectId)。每个类都有一个工厂方法
,因此典型的工作流程是:
Suppose there is IMember so
Member implements IMember
. Getter methods are defined in IMember.Another class
MorphiaMember implements IMember
is annotated as necessary and has ID field (id is not always ObjectId).Each class has a factory method
so typical workflow will be: