了解 Orchard 连接和数据关系
在 Orchard 中,模块开发人员如何能够了解“连接”的工作原理,特别是在连接到核心部分和记录时?我见过的更好的帮助之一是 Orchard 文档,但这些例子都没有展示如何与现有或核心部分建立关系。作为我正在寻找的示例,这里是取自工作示例的模块服务代码片段:
_contentManager
.Query<TaxonomyPart>()
.Join<RoutePartRecord>()
.Where(r => r.Title == name)
.List()
在本例中,自定义 TaxonomyPart
正在与核心 RoutePartRecord
连接代码>.我研究了代码,但看不出 TaxononmyPart 如何“连接”到 RoutePartRecord。同样,从工作代码中,这里是另一个片段驱动程序代码,它将自定义 TagsPart 与核心 CommonPartRecord 相关联:
List<string> tags = new List<string> { "hello", "there" };
IContentQuery<TagsPart, TagsPartRecord> query = _cms.Query<TagsPart, TagsPartRecord>();
query.Where(tpr => tpr.Tags.Any(t => tags.Contains(t.TagRecord.TagName)));
IEnumerable<TagsPart> parts =
query.Join<CommonPartRecord>()
.Where(cpr => cpr.Id != currentItemId)
.OrderByDescending(cpr => cpr.PublishedUtc)
.Slice(part.MaxItems);
我认为我可以从前面的任何一个示例中学习如何形成我自己的查询。我这样做了:
List<string> tags = new List<string> { "hello", "there" };
IContentQuery<TagsPart, TagsPartRecord> query = _cms.Query<TagsPart, TagsPartRecord>();
query.Where(tpr => tpr.Tags.Any(t => tags.Contains(t.TagRecord.TagName)));
var stuff =
query.Join<ContainerPartRecord>()
.Where(ctrPartRecord => ctrPartRecord.ContentItemRecord.ContentType.Name == "Primary")
.List();
我的代码的目的是将找到的内容项限制为仅特定容器(或博客)的内容项。当代码运行时,它在我的连接查询上引发异常,提示 {"could not parse property: ContentType of: Orchard.Core.Containers.Models.ContainerPartRecord"}
。这会导致各种问题:
- 为什么在第二个示例的驱动程序的 Display() 方法中填充的是 CommonPartRecord,而不是 ContainerPartRecord?一般来说,我如何知道填充了哪些部分记录以及何时填充?
- 在工作代码片段中,由于没有指定连接键/条件(并且没有明显的隐式连接键),连接到底如何工作?例如,我检查了数据迁移文件和
models
类,发现TagsPart和CommonPartRecord之间没有内在联系。因此,除了查看示例代码之外,人们如何首先知道这样的连接是合法的或可能的? - 我尝试使用
TagsPart
和ContainerPartRecord
进行的连接在任何情况下都合法吗?哪个? - 这些示例的查询语法主要是 Orchard、NHibernate 或 LINQ to NHibernate 的反映吗?如果它主要是 NHibernate 的反映,那么推荐阅读哪本 NHibernate 书籍或文章,以便我可以更深入地了解 Orchard?
文档中似乎存在关于此类想法和问题的漏洞,这使得编写模块变得困难。无论这个主题能找到什么答案,我都很乐意将其编译成一篇文章或社区 Orchard 文档。
In Orchard, how is a module developer able to learn how "joins" work, particularly when joining to core parts and records? One of the better helps I've seen was in Orchard documentation, but none of those examples show how to form relations with existing or core parts. As an example of something I'm looking for, here is a snippet of module service code taken from a working example:
_contentManager
.Query<TaxonomyPart>()
.Join<RoutePartRecord>()
.Where(r => r.Title == name)
.List()
In this case, a custom TaxonomyPart
is joining with a core RoutePartRecord
. I've investigated the code, and I can't see how that a TaxononmyPart is "joinable" to a RoutePartRecord. Likewise, from working code, here is another snippet driver code which relates a custom TagsPart with a core CommonPartRecord:
List<string> tags = new List<string> { "hello", "there" };
IContentQuery<TagsPart, TagsPartRecord> query = _cms.Query<TagsPart, TagsPartRecord>();
query.Where(tpr => tpr.Tags.Any(t => tags.Contains(t.TagRecord.TagName)));
IEnumerable<TagsPart> parts =
query.Join<CommonPartRecord>()
.Where(cpr => cpr.Id != currentItemId)
.OrderByDescending(cpr => cpr.PublishedUtc)
.Slice(part.MaxItems);
I thought I could learn from either of the prior examples of how to form my own query. I did this:
List<string> tags = new List<string> { "hello", "there" };
IContentQuery<TagsPart, TagsPartRecord> query = _cms.Query<TagsPart, TagsPartRecord>();
query.Where(tpr => tpr.Tags.Any(t => tags.Contains(t.TagRecord.TagName)));
var stuff =
query.Join<ContainerPartRecord>()
.Where(ctrPartRecord => ctrPartRecord.ContentItemRecord.ContentType.Name == "Primary")
.List();
The intent of my code is to limit the content items found to only those of a particular container (or blog). When the code ran, it threw an exception on my join query saying {"could not resolve property: ContentType of: Orchard.Core.Containers.Models.ContainerPartRecord"}
. This leads to a variety of questions:
- Why in the driver's Display() method of the second example is the
CommonPartRecord
populated, but not theContainerPartRecord
? In general how would I know what part records are populated, and when? - In the working code snippets, how exactly is the join working since no join key/condition is specified (and no implicit join keys are apparent)? For example, I checked the data migration file and
models
classes, and found no inherent relation between a TagsPart and a CommonPartRecord. Thus, besides looking at that sample code, how would anyone have known in the first place that such a join was legal or possible? - Is the join I tried with
TagsPart
andContainerPartRecord
legal in any context? Which? - Is the query syntax of these examples primarily a reflection of Orchard, of NHibernate, or LINQ to NHibernate? If it is primarily a reflection of NHibernate, then which NHibernate book or article is recommended reading so that I can dig deeper into Orchard?
It seems there is a hole in the documentation regarding these kinds of thoughts and questions, which makes it hard to write a module. Whatever answers can be found for this topic, I'd be glad to compile into an article or community Orchard documentation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至于文档中的漏洞,好吧,但是当然。我们今天拥有的文档仅涵盖了该平台的一小部分。今天最好的参考是源代码。我们和社区其他成员都会高度赞赏您为此做出的任何贡献。如果您需要这方面的帮助,请告诉我。
As for holes in the documentation, well, but of course. The documentation that we have today is only covering a very small part of the platform. Your best reference today is the source code. Any contribution you could make to this is highly appreciated by us and by the rest of the community. Let me know if you need help with this.