问题描述
我有一个Workspace
和Document
实体,一个工作区可以包含零个,一个或多个文档的想法.我对此建模的第一种方法是:
I have a Workspace
and Document
entities, with the idea that a workspace can contain zero, one, or more documents. My first approach to model this was:
case class Workspace(name: String, documents: Seq[Document])
但是这不能很好地扩展,因为我的工作区可能包含许多个文档.幸运的是,我的业务需求使我能够分别处理工作空间和文档(从某种意义上说,当我拥有工作空间时,没有任何理由或不变性迫使我考虑其中包含的所有文档).
but this will not scale well since my workspaces may contain many documents. Fortunately, my business requirement allow me to treat workspaces and documents separately (in the sense that when I have a workspace, there is no reason or invariant that forces me to consider all documents contained in it).
问题::我想知道:如何在Workspace和Document
="nofollow"> Sorm ,这样我就可以在两者之间建立链接,而不必加载工作区的所有文档?我想象有一个存储库,该存储库将使我能够访问工作区的文档,并支持分页.)
Question: I am wondering: how would I model Workspace
and Document
in Sorm so that I have a link between the two but do not have to load all documents of a workspace? I imagine to have a Repository that would give me access to the documents of a workspace, with pagination support.)
case class Workspace(name: String)
case class Document(name: String, /* ... */)
trait WorkspaceRepository {
def children(ws: Workspace, offset: Long, limit: Long)
}
推荐答案
轻松自如!您将它们定义为不相关:
Easy peasy! You define them unrelated:
case class Workspace ( name : String )
case class Document ( ... )
然后,您选择希望它们链接的方式.我看到两个.
Then you choose a way you wish them to be linked. I see two.
case class WorkspaceDocuments
( workspace : Workspace, documents : Seq[Document] )
并获得工作区的所有文档,如下所示:
And get all documents of a workspace like so:
Db.query[WorkspaceDocuments]
.whereEqual("workspace", theWorkspace)
.fetchOne()
.map(_.documents)
.getOrElse(Seq())
在这种情况下,在实例声明中将workspace
属性指定为唯一是有意义的:
In this case it makes sense to specify the workspace
property as unique in instance declaration:
... Instance (
entities = Set() +
Entity[WorkspaceDocuments]( unique = Set() + Seq("workspace") )
...
)
Variant#2
case class WorkspaceToDocument
( workspace : Workspace, document : Document )
并像这样获取工作区的文档:
And get documents of a workspace like so:
Db.query[WorkspaceToDocument]
.whereEqual("workspace", theWorkspace)
.whereEqual("document.name", "...") // ability to filter docs
.fetch()
.map(_.document)
第一个变体不能让您过滤查询中的文档(至少在SORM v0.3.*中),但是由于能够在工作空间上设置唯一约束,因此它在基于工作空间的查询上应该表现更好.第二种变体更加灵活,允许您应用各种过滤器.
First variant won't let you filter docs in your query (at least in SORM v0.3.*) but due to ability to set a unique constraint on a workspace it should perform better on workspace-based queries. The second variant is more flexible, allowing you to apply all kinds of filters.
这篇关于如何在Sorm中为有多个孩子的实体建模?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!