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.
This commit is contained in:
shannon@ShandemVaio
2012-07-21 00:59:27 +06:00
parent 36e3857643
commit d9faea49ec
3 changed files with 54 additions and 64 deletions

View File

@@ -8,33 +8,49 @@ using umbraco.BusinessLogic.Utils;
namespace Umbraco.Web
{
/// <summary>
/// Extension methods for the PluginResolver
/// </summary>
public static class PluginResolverExtensions
{
/// Extension methods for the PluginResolver
/// </summary>
public static class PluginResolverExtensions
{
private static volatile IEnumerable<Type> _lookups;
private static readonly object Locker = new object();
private static volatile IEnumerable<ILookup> _lookups;
private static readonly object Locker = new object();
/// <summary>
/// Returns all available ILookup objects
/// </summary>
/// <param name="plugins"></param>
/// <returns></returns>
internal static IEnumerable<Type> ResolveLookups(this PluginResolver plugins)
{
if (_lookups == null)
{
lock(Locker)
{
if (_lookups == null)
{
_lookups = TypeFinder.FindClassesOfType<ILookup>();
}
}
}
return _lookups;
}
/// <summary>
/// Returns all available ILookup objects
/// </summary>
/// <param name="plugins"></param>
/// <returns></returns>
internal static IEnumerable<ILookup> ResolveLookups(this PluginResolver plugins)
{
if (_lookups == null)
{
lock (Locker)
{
if (_lookups == null)
{
var lookupTypes = TypeFinder.FindClassesOfType<ILookup>();
var lookups = new List<ILookup>();
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;
}
}
}
}

View File

@@ -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

View File

@@ -7,11 +7,11 @@ using Umbraco.Core;
namespace Umbraco.Web.Routing
{
/// <summary>
/// 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
/// </summary>
internal class RouteLookups
{
private static readonly List<Type> Lookups = new List<Type>();
private static readonly List<ILookup> Lookups = new List<ILookup>();
private static readonly ReaderWriterLockSlim Lock = new ReaderWriterLockSlim();
/// <summary>
@@ -19,7 +19,7 @@ namespace Umbraco.Web.Routing
/// </summary>
public static RouteLookups Current { get; internal set; }
internal RouteLookups(IEnumerable<Type> lookups)
internal RouteLookups(IEnumerable<ILookup> lookups)
{
Lookups.AddRange(SortByWeight(lookups));
}
@@ -28,7 +28,7 @@ namespace Umbraco.Web.Routing
/// Returns all of the lookups
/// </summary>
/// <returns></returns>
public IEnumerable<Type> GetLookups()
public IEnumerable<ILookup> GetLookups()
{
return Lookups;
}
@@ -46,25 +46,13 @@ namespace Umbraco.Web.Routing
}
}
/// <summary>
/// Adds a new lookup to the end of the list
/// </summary>
/// <typeparam name="T"></typeparam>
public void AddLookup<T>()
where T : ILookup
{
AddLookup(typeof (T));
}
/// <summary>
/// Adds a new lookup to the end of the list
/// </summary>
/// <param name="lookup"></param>
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
/// </summary>
/// <param name="index"></param>
/// <param name="lookup"></param>
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
/// </summary>
/// <param name="lookup"></param>
/// <returns></returns>
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());
}
/// <summary>
@@ -105,11 +93,11 @@ namespace Umbraco.Web.Routing
/// </summary>
/// <param name="lookups"></param>
/// <returns></returns>
private static IEnumerable<Type> SortByWeight(IEnumerable<Type> lookups)
private static IEnumerable<ILookup> SortByWeight(IEnumerable<ILookup> lookups)
{
return lookups.OrderBy(x =>
{
var attribute = x.GetCustomAttributes(true).OfType<LookupWeightAttribute>().SingleOrDefault();
var attribute = x.GetType().GetCustomAttributes(true).OfType<LookupWeightAttribute>().SingleOrDefault();
return attribute == null ? LookupWeightAttribute.DefaultWeight : attribute.Weight;
}).ToList();
}