Skip to content

Commit

Permalink
Using Default Language to look up base templates. (#39)
Browse files Browse the repository at this point in the history
* Using Glass' VersionCountDisabler to retrieve shared version of item for reading base-templates
* Consolidating using statements to reduce nesting.
  • Loading branch information
kmazzoni authored and smithc committed Mar 21, 2017
1 parent 5789783 commit cd2dc31
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions Jabberwocky.Glass/Factory/Caching/GlassTemplateCacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<Type, string>(interfaceType, templateKey), key =>
{
// Otherwise, search for match, and update 1st-level cache
using (var service = _serviceFactory())
{
foreach (Guid baseTemplateId in GetBaseTemplates(service.GetItem<IBaseTemplates>(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<IBaseTemplates>(templateId), service, depth))
{
string templateIdString = baseTemplateId.ToString();
if (itemInterfaces.ContainsKey(templateIdString))
{
return itemInterfaces[templateIdString].ImplementationType;
}
}
}

return null;
});
Expand All @@ -98,14 +100,15 @@ public Type GetFallbackImplementingTypeForTemplate(Guid templateId, Type interfa
return null;
}

using (var service = _serviceFactory())
{
var currentTemplate = service.GetItem<IBaseTemplates>(templateId);
using (var service = _serviceFactory())
using (new VersionCountDisabler())
{
var currentTemplate = service.GetItem<IBaseTemplates>(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<Guid> GetBaseTemplates(IBaseTemplates item, ISitecoreService service, int depth = DefaultDepth, bool ignoreTemplate = false)
Expand Down

0 comments on commit cd2dc31

Please sign in to comment.