From cd2dc316d4732a0dbd14d40727828214e8f27bc7 Mon Sep 17 00:00:00 2001 From: Kevin Mazzoni Date: Tue, 21 Mar 2017 12:28:55 -0400 Subject: [PATCH] Using Default Language to look up base templates. (#39) * Using Glass' VersionCountDisabler to retrieve shared version of item for reading base-templates * Consolidating using statements to reduce nesting. --- .../Caching/GlassTemplateCacheService.cs | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/Jabberwocky.Glass/Factory/Caching/GlassTemplateCacheService.cs b/Jabberwocky.Glass/Factory/Caching/GlassTemplateCacheService.cs index cc1c84b..b6034a1 100644 --- a/Jabberwocky.Glass/Factory/Caching/GlassTemplateCacheService.cs +++ b/Jabberwocky.Glass/Factory/Caching/GlassTemplateCacheService.cs @@ -6,6 +6,7 @@ using Glass.Mapper.Sc.Configuration.Attributes; using Jabberwocky.Glass.Factory.Util; using Jabberwocky.Glass.Models; +using Sitecore.Data.Managers; namespace Jabberwocky.Glass.Factory.Caching { @@ -74,18 +75,19 @@ private Type InnerGetImplementingTypeForTemplate(Guid templateId, Type interface // If no exact match exists, attempt to resolve from 1st-level cache return _firstLevelCache.GetOrAdd(new Tuple(interfaceType, templateKey), key => { - // Otherwise, search for match, and update 1st-level cache - using (var service = _serviceFactory()) - { - foreach (Guid baseTemplateId in GetBaseTemplates(service.GetItem(templateId), service, depth)) - { - string templateIdString = baseTemplateId.ToString(); - if (itemInterfaces.ContainsKey(templateIdString)) - { - return itemInterfaces[templateIdString].ImplementationType; - } - } - } + // Otherwise, search for match, and update 1st-level cache + using (var service = _serviceFactory()) + using (new VersionCountDisabler()) + { + foreach (Guid baseTemplateId in GetBaseTemplates(service.GetItem(templateId), service, depth)) + { + string templateIdString = baseTemplateId.ToString(); + if (itemInterfaces.ContainsKey(templateIdString)) + { + return itemInterfaces[templateIdString].ImplementationType; + } + } + } return null; }); @@ -98,14 +100,15 @@ public Type GetFallbackImplementingTypeForTemplate(Guid templateId, Type interfa return null; } - using (var service = _serviceFactory()) - { - var currentTemplate = service.GetItem(templateId); + using (var service = _serviceFactory()) + using (new VersionCountDisabler()) + { + var currentTemplate = service.GetItem(templateId); - return GetBaseTemplates(currentTemplate, service, depth: MaxDepth) - .Select(guid => InnerGetImplementingTypeForTemplate(guid, interfaceType, 1)) - .FirstOrDefault(type => type != null); - } + return GetBaseTemplates(currentTemplate, service, depth: MaxDepth) + .Select(guid => InnerGetImplementingTypeForTemplate(guid, interfaceType, 1)) + .FirstOrDefault(type => type != null); + } } internal IEnumerable GetBaseTemplates(IBaseTemplates item, ISitecoreService service, int depth = DefaultDepth, bool ignoreTemplate = false)