Sitefinity:使用字段值优化动态内容查询

本文关键字:动态 查询 优化 字段 Sitefinity | 更新日期: 2023-09-27 18:33:07

我会尽量具体。所以我们使用的是Sitefinity 8.1.5800,我有几个动态内容模块,名为ReleaseNotes和ReleaseNoteItems。发行说明有一些字段,但没有对发行说明项的引用。

发行说明

项具有与发行说明相关的字段和相关的数据字段。

因此,我可以在不到一秒钟的时间内将所有 ReleaseNoteItems 作为动态内容进行查询。

然后,我使用 sitefinity 提供的这些对象,并将它们映射到 C# 对象,以便我可以使用强类型。此映射过程几乎需要一分钟,并且仅对 322 个项目 (N+1( 使用了 600 多个查询。

简而言之:我需要获取所有 sitefinity 对象并将它们映射到可用的 c# 对象,比我现在更快。

获取动态内容项的方法(需要毫秒(:


private IList<DynamicContent> GetAllLiveReleaseNoteItemsByReleaseNoteParentId(Guid releaseNoteParentId)
       {
           DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(String.Empty);
           Type releasenoteitemType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.ReleaseNoteItems.Releasenoteitem");
           string releaseNoteParentTypeString = "Telerik.Sitefinity.DynamicTypes.Model.ReleaseNotes.Releasenote";
           var provider = dynamicModuleManager.Provider as OpenAccessDynamicModuleProvider;
           int? totalCount = 0;
           var cultureName = "en";
           Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);

           Type releasenoteType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.ReleaseNotes.Releasenote");
           // This is how we get the releasenote items through filtering
           DynamicContent myCurrentItem = dynamicModuleManager.GetDataItem(releasenoteType, releaseNoteParentId);
           var myMasterParent =
               dynamicModuleManager.Lifecycle.GetMaster(myCurrentItem) as DynamicContent;
           var relatingItems = provider.GetRelatedItems(
                releaseNoteParentTypeString,
                "OpenAccessProvider",
                myMasterParent.Id,
                string.Empty,
                releasenoteitemType,
                ContentLifecycleStatus.Live,
                string.Empty,
                string.Empty,
                null,
                null,
                ref totalCount,
                RelationDirection.Parent).OfType<DynamicContent>();
           IList<DynamicContent> allReleaseNoteItems = relatingItems.ToList();
           return allReleaseNoteItems;
       }

这是将 sitefinity 对象映射到 C# 对象需要将近一分钟的方法:

public IList<ReleaseNoteItemModel> GetReleaseNoteItemsByReleaseNoteParent(ReleaseNoteModel releaseNoteItemParent)
       {
          return  GetAllLiveReleaseNoteItemsByReleaseNoteParentId(releaseNoteItemParent.Id).Select(rn => new ReleaseNoteItemModel
           {
               Id = rn.Id,
               Added = rn.GetValue("Added") is bool ? (bool)rn.GetValue("Added") : false,
               BugId = rn.GetValue<string>("bug_id"),
               BugStatus = rn.GetValue<Lstring>("bugStatus"),
               Category = rn.GetValue<Lstring>("category"),
               Component = rn.GetValue<Lstring>("component"),
               @Content = rn.GetValue<Lstring>("content"),
               Criticality = rn.GetValue<Lstring>("criticality"),
               Customer = rn.GetValue<string>("customer"),
               Date = rn.GetValue<DateTime?>("date"),
               Grouped = rn.GetValue<string>("grouped"),
               Override = rn.GetValue<string>("override"),
               Patch_Num = rn.GetValue<string>("patch_num"),
               PublishedDate = rn.PublicationDate,
               Risk = rn.GetValue<Lstring>("risk"),
               Title = rn.GetValue<string>("Title"),
               Summary = rn.GetValue<Lstring>("summary"),
               Prod_Name = rn.GetValue<Lstring>("prod_name"),
               ReleaseNoteParent = releaseNoteItemParent,
               McProductId = GetMcProductId(rn.GetRelatedItems("McProducts").Cast<DynamicContent>()),
           }).ToList();
       }

有没有办法将这一切优化到一个查询中或更好的方法?花费将近一分钟来映射这些对象对于我们需要对它们执行的操作来说太长了。

如果没有办法,我们将必须缓存项目或进行SQL查询。如果没有必要,我宁愿不做缓存或SQL查询。

提前感谢您提供的任何和所有帮助,我是在stackoverflow上发布问题的新手,所以如果您需要任何其他数据,请告诉我。

Sitefinity:使用字段值优化动态内容查询

你为什么要对这些项目进行.ToList()吗?你有没有可能避免这种情况。在我看来,大部分时间(1 分钟(用于将所有项目转换为列表。从Sitefinity对象到C#对象的转换并不是这里的罪魁祸首。

看看Arno的回答:https://plus.google.com/u/0/112295105425490148444/posts/QrsVtxj1sCB?cfem=1

您可以使用"内容链接管理器"更快地查询动态模块关系(通过父 -ParentItemId- 或子 -ChildItemId-(:

var providerName = String.Empty;
var parentTitle = "Parent";
var relatedTitle = "RelatedItem3";
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
Type parentType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.ParentModules.ParentModule");
Type relatedType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.RelatedModules.RelatedModule");
ContentLinksManager contentLinksManager = ContentLinksManager.GetManager();
// get the live version of all parent items
var parentItems = dynamicModuleManager.GetDataItems(parentType).Where(i => i.GetValue<string>("Title").Contains(parentTitle) && i.Status == ContentLifecycleStatus.Live && i.Visible);
// get the ids of the related items.
// We use the OriginalContentId property since we work with the live vesrions of the dynamic modules
var parentItemIds = parentItems.Select(i => i.OriginalContentId).ToList();
// get the live versions of all the schedules items
var relatedItems = dynamicModuleManager.GetDataItems(relatedType).Where(i => i.Status == ContentLifecycleStatus.Live && i.Visible && i.GetValue<string>("Title").Contains(relatedTitle));
// get the content links
var contentLinks = contentLinksManager.GetContentLinks().Where(cl => cl.ParentItemType == parentType.FullName && cl.ComponentPropertyName == "RelatedField" && parentItemIds.Contains(cl.ParentItemId) && cl.AvailableForLive);
// get the IDs of the desired parent items
var filteredParentItemIds = contentLinks.Join<ContentLink, DynamicContent, Guid, Guid>(relatedItems, (cl) => cl.ChildItemId, (i) => i.OriginalContentId, (cl, i) => cl.ParentItemId).Distinct();
// get the desired parent items by the filtered IDs
var filteredParentItems = parentItems.Where(i => filteredParentItemIds.Contains(i.OriginalContentId)).ToList();
我想单个

发行说明下的每个发行说明项目都与同一产品相关,不是吗?

如果是这样,您是否需要为每个项目执行 GetMcProductId 方法?