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

时间:2016-02-03 15:30:23

标签: c# sitefinity

我会尝试尽可能具体。所以我们使用 Sitefinity 8.1.5800 ,我有几个名为ReleaseNotes和ReleaseNoteItems的动态内容模块。 ReleaseNotes有一些字段但没有引用ReleaseNoteItems。

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

因此,我可以在不到一秒的时间内将所有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的问题,所以如果您需要任何其他数据,请告诉我们。

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

我会想象单个发行说明下的每个发行说明项目都与相同的产品有关吗?

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

答案 2 :(得分:0)

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

您可以使用&#34;内容链接管理器&#34;查询动态模块关系(由父-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();