From d9faea49ec6f8184aa263d8089f03fb3b5e01420 Mon Sep 17 00:00:00 2001 From: "shannon@ShandemVaio" Date: Sat, 21 Jul 2012 00:59:27 +0600 Subject: [PATCH] Changed the ILookup resolution, now that DocumentRequest object contains everything to do the lookups we don't have to create each ILookup per request, these can exist one per application. --- src/Umbraco.Web/PluginResolverExtensions.cs | 68 +++++++++++++-------- src/Umbraco.Web/Routing/DocumentRequest.cs | 16 +---- src/Umbraco.Web/Routing/RouteLookups.cs | 34 ++++------- 3 files changed, 54 insertions(+), 64 deletions(-) diff --git a/src/Umbraco.Web/PluginResolverExtensions.cs b/src/Umbraco.Web/PluginResolverExtensions.cs index 9bbab2bb1c..381fad68fb 100644 --- a/src/Umbraco.Web/PluginResolverExtensions.cs +++ b/src/Umbraco.Web/PluginResolverExtensions.cs @@ -8,33 +8,49 @@ using umbraco.BusinessLogic.Utils; namespace Umbraco.Web { /// - /// Extension methods for the PluginResolver - /// - public static class PluginResolverExtensions - { + /// Extension methods for the PluginResolver + /// + public static class PluginResolverExtensions + { - private static volatile IEnumerable _lookups; - private static readonly object Locker = new object(); + private static volatile IEnumerable _lookups; + private static readonly object Locker = new object(); - /// - /// Returns all available ILookup objects - /// - /// - /// - internal static IEnumerable ResolveLookups(this PluginResolver plugins) - { - if (_lookups == null) - { - lock(Locker) - { - if (_lookups == null) - { - _lookups = TypeFinder.FindClassesOfType(); - } - } - } - return _lookups; - } + /// + /// Returns all available ILookup objects + /// + /// + /// + internal static IEnumerable ResolveLookups(this PluginResolver plugins) + { + if (_lookups == null) + { + lock (Locker) + { + if (_lookups == null) + { + var lookupTypes = TypeFinder.FindClassesOfType(); + var lookups = new List(); + foreach (var l in lookupTypes) + { + try + { + var typeInstance = Activator.CreateInstance(l) as ILookup; + lookups.Add(typeInstance); + } + catch (Exception ex) + { + //TODO: Need to fix logging so this doesn't bork if no SQL connection + //Log.Add(LogTypes.Error, -1, "Error loading ILookup: " + ex.ToString()); + } + } + //set the global + _lookups = lookups; + } + } + } + return _lookups; + } - } + } } \ No newline at end of file diff --git a/src/Umbraco.Web/Routing/DocumentRequest.cs b/src/Umbraco.Web/Routing/DocumentRequest.cs index af62b651ef..b40df5bb33 100644 --- a/src/Umbraco.Web/Routing/DocumentRequest.cs +++ b/src/Umbraco.Web/Routing/DocumentRequest.cs @@ -247,21 +247,7 @@ namespace Umbraco.Web.Routing // some lookups may implement caching Trace.TraceInformation("{0}Begin lookup", tracePrefix); var lookups = RoutingContext.RouteLookups.GetLookups(); - lookups.Any(lookup => - { - //create the instance - try - { - var instance = (ILookup)Activator.CreateInstance(lookup); - return instance.LookupDocument(this); - } - catch (Exception ex) - { - Log.Add(LogTypes.Error, -1, "Error loading ILookup: " + ex.ToString()); - return false; - } - }); - + lookups.Any(lookup => lookup.LookupDocument(this)); Trace.TraceInformation("{0}End lookup, {1}", tracePrefix, (this.HasNode ? "a document was found" : "no document was found")); // fixme - not handling umbracoRedirect diff --git a/src/Umbraco.Web/Routing/RouteLookups.cs b/src/Umbraco.Web/Routing/RouteLookups.cs index 6dd33576e4..137ea8514d 100644 --- a/src/Umbraco.Web/Routing/RouteLookups.cs +++ b/src/Umbraco.Web/Routing/RouteLookups.cs @@ -7,11 +7,11 @@ using Umbraco.Core; namespace Umbraco.Web.Routing { /// - /// Represents a collection of ILookup types used for routing that are registered in the application + /// Represents a collection of ILookup used for routing that are registered in the application /// internal class RouteLookups { - private static readonly List Lookups = new List(); + private static readonly List Lookups = new List(); private static readonly ReaderWriterLockSlim Lock = new ReaderWriterLockSlim(); /// @@ -19,7 +19,7 @@ namespace Umbraco.Web.Routing /// public static RouteLookups Current { get; internal set; } - internal RouteLookups(IEnumerable lookups) + internal RouteLookups(IEnumerable lookups) { Lookups.AddRange(SortByWeight(lookups)); } @@ -28,7 +28,7 @@ namespace Umbraco.Web.Routing /// Returns all of the lookups /// /// - public IEnumerable GetLookups() + public IEnumerable GetLookups() { return Lookups; } @@ -46,25 +46,13 @@ namespace Umbraco.Web.Routing } } - /// - /// Adds a new lookup to the end of the list - /// - /// - public void AddLookup() - where T : ILookup - { - AddLookup(typeof (T)); - } - + /// /// Adds a new lookup to the end of the list /// /// - public void AddLookup(Type lookup) + public void AddLookup(ILookup lookup) { - if (!typeof(ILookup).IsAssignableFrom(lookup)) - throw new InvalidOperationException("The type specified is not of type " + typeof (ILookup)); - if (CheckExists(lookup)) throw new InvalidOperationException("The lookup type " + lookup + " already exists in the lookup collection"); @@ -79,7 +67,7 @@ namespace Umbraco.Web.Routing /// /// /// - public void InsertLookup(int index, Type lookup) + public void InsertLookup(int index, ILookup lookup) { if (CheckExists(lookup)) throw new InvalidOperationException("The lookup type " + lookup + " already exists in the lookup collection"); @@ -95,9 +83,9 @@ namespace Umbraco.Web.Routing /// /// /// - private static bool CheckExists(Type lookup) + private static bool CheckExists(ILookup lookup) { - return Lookups.Any(x => x == lookup); + return Lookups.Any(x => x.GetType() == lookup.GetType()); } /// @@ -105,11 +93,11 @@ namespace Umbraco.Web.Routing /// /// /// - private static IEnumerable SortByWeight(IEnumerable lookups) + private static IEnumerable SortByWeight(IEnumerable lookups) { return lookups.OrderBy(x => { - var attribute = x.GetCustomAttributes(true).OfType().SingleOrDefault(); + var attribute = x.GetType().GetCustomAttributes(true).OfType().SingleOrDefault(); return attribute == null ? LookupWeightAttribute.DefaultWeight : attribute.Weight; }).ToList(); }