Core.ObjectsResolution - refactor, cleanup, sanitize, ensure we properly freeze

This commit is contained in:
Stephan
2013-01-16 13:31:04 -01:00
parent bfd8e96f71
commit 01b4e8d59c
29 changed files with 151 additions and 120 deletions

View File

@@ -149,9 +149,8 @@ namespace Umbraco.Core.ObjectResolution
{
EnsureAddSupport();
EnsureResolutionNotFrozen();
using (GetWriteLock())
using (Resolution.Configuration)
using (GetWriteLock())
{
foreach (var t in types)
{
@@ -168,9 +167,8 @@ namespace Umbraco.Core.ObjectResolution
{
EnsureAddSupport();
EnsureResolutionNotFrozen();
using (GetWriteLock())
using (Resolution.Configuration)
using (GetWriteLock())
{
_listOfTypeListDelegates.Add(typeListDelegate);
}
@@ -184,9 +182,8 @@ namespace Umbraco.Core.ObjectResolution
{
EnsureAddSupport();
EnsureResolutionNotFrozen();
using (GetWriteLock())
using (Resolution.Configuration)
using (GetWriteLock())
{
_lazyTypeList.Add(value);
}
@@ -208,9 +205,8 @@ namespace Umbraco.Core.ObjectResolution
{
EnsureClearSupport();
EnsureResolutionNotFrozen();
using (GetWriteLock())
using (Resolution.Configuration)
using (GetWriteLock())
{
_lazyTypeList.Clear();
}

View File

@@ -7,10 +7,10 @@ using System.Threading;
namespace Umbraco.Core.ObjectResolution
{
/// <summary>
/// A base resolver used for old legacy factories such as the DataTypeFactory or CacheResolverFactory.
/// The base class for old legacy factories such as the DataTypeFactory or CacheResolverFactory.
/// </summary>
/// <typeparam name="TResolver"></typeparam>
/// <typeparam name="TResolved"> </typeparam>
/// <typeparam name="TResolver">The type of the concrete resolver class.</typeparam>
/// <typeparam name="TResolved">The type of the resolved objects.</typeparam>
/// <remarks>
/// This class contains basic functionality to mimic the functionality in these old factories since they all return
/// transient objects (though this should be changed) and the method GetById needs to lookup a type to an ID and since
@@ -21,7 +21,9 @@ namespace Umbraco.Core.ObjectResolution
where TResolved : class
where TResolver : class
{
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
private ConcurrentDictionary<Guid, Type> _id2type;
#region Constructors
/// <summary>
@@ -40,46 +42,45 @@ namespace Umbraco.Core.ObjectResolution
#endregion
/// <summary>
/// Maintains a list of Ids and their types when first call to CacheResolvers or GetById occurs, this is used
/// in order to return a single object by id without instantiating the entire type stack.
/// Returns the unique identifier of the type of a specified object.
/// </summary>
private ConcurrentDictionary<Guid, Type> _trackIdToType;
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
/// <param name="value">The object.</param>
/// <returns>The unique identifier of the type of <paramref name="value"/>.</returns>
protected abstract Guid GetUniqueIdentifier(TResolved value);
/// <summary>
/// method to return the unique id for type T
/// Returns a new instance for the type identified by its unique type identifier.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
protected abstract Guid GetUniqueIdentifier(TResolved obj);
/// <summary>
/// Returns a new TResolved instance by id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
/// <param name="id">The type identifier.</param>
/// <returns>The value of the type uniquely identified by <paramref name="id"/>.</returns>
public TResolved GetById(Guid id)
{
EnsureIdsAreTracked();
return !_trackIdToType.ContainsKey(id)
EnsureIsInitialized();
return !_id2type.ContainsKey(id)
? null
: PluginManager.Current.CreateInstance<TResolved>(_trackIdToType[id]);
: PluginManager.Current.CreateInstance<TResolved>(_id2type[id]);
}
/// <summary>
/// Populates the ids -> Type dictionary to allow us to instantiate a type by Id since these legacy types doesn't contain any metadata
/// Populates the identifiers-to-types dictionnary.
/// </summary>
protected void EnsureIdsAreTracked()
/// <remarks>
/// <para>This allow us to instantiate a type by ID since these legacy types doesn't contain any metadata.</para>
/// <para>We instanciate all types once to get their unique identifier, then build the dictionary so that
/// when GetById is called, we can instanciate a single object.</para>
/// </remarks>
protected void EnsureIsInitialized()
{
using (var l = new UpgradeableReadLock(_lock))
{
if (_trackIdToType == null)
if (_id2type == null)
{
l.UpgradeToWriteLock();
_trackIdToType = new ConcurrentDictionary<Guid, Type>();
foreach (var v in Values)
_id2type = new ConcurrentDictionary<Guid, Type>();
foreach (var value in Values)
{
_trackIdToType.TryAdd(GetUniqueIdentifier(v), v.GetType());
_id2type.TryAdd(GetUniqueIdentifier(value), value.GetType());
}
}
}

View File

@@ -1,94 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Umbraco.Core.Macros;
using umbraco.interfaces;
namespace Umbraco.Core.ObjectResolution
{
// FIXME - specific resolvers should not be in Umbraco.Core.ObjectResolution ??
/// <summary>
/// A resolver to return all IMacroGuiRendering objects
/// </summary>
/// <remarks>
/// Much of this classes methods are based on legacy code from umbraco.editorControls.macrocontainer.MacroControlFactory
/// this code should probably be reviewed and cleaned up if necessary.
/// </remarks>
internal sealed class MacroFieldEditorsResolver : LazyManyObjectsResolverBase<MacroFieldEditorsResolver, IMacroGuiRendering>
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="macroEditors"></param>
internal MacroFieldEditorsResolver(Func<IEnumerable<Type>> macroEditors)
: base(macroEditors, ObjectLifetimeScope.Transient)
{
}
/// <summary>
/// Gets the <see cref="IMacroGuiRendering"/> implementations.
/// </summary>
public IEnumerable<IMacroGuiRendering> MacroFieldEditors
{
get
{
return Values;
}
}
/// <summary>
/// Gets the value based on the type of control
/// </summary>
/// <param name="macroControl"></param>
/// <returns></returns>
/// <remarks>
/// This is legacy code migrated from umbraco.editorControls.macrocontainer.MacroControlFactory
/// </remarks>
internal string GetValueFromMacroControl(Control macroControl)
{
return HttpUtility.HtmlDecode(((IMacroGuiRendering)macroControl).Value);
}
/// <remarks>
/// This is legacy code migrated from umbraco.editorControls.macrocontainer.MacroControlFactory
/// </remarks>
internal List<Type> MacroControlTypes
{
get { return InstanceTypes.ToList(); }
}
/// <summary>
/// Create an instance of a Macro control and return it.
/// Because the macro control uses inline client script whichs is not generated after postback
/// That's why we use the Page Picker instead of the content picker of the macro.
/// </summary>
/// <remarks>
/// This is legacy code migrated from umbraco.editorControls.macrocontainer.MacroControlFactory
/// </remarks>
internal Control GetMacroRenderControlByType(PersistableMacroProperty prop, string uniqueId)
{
var m = MacroControlTypes.FindLast(macroGuiCcontrol => macroGuiCcontrol.ToString() == string.Format("{0}.{1}", prop.AssemblyName, prop.TypeName));
var instance = PluginManager.Current.CreateInstance<IMacroGuiRendering>(m);
if (instance != null)
{
if (!string.IsNullOrEmpty(prop.Value))
{
instance.Value = HttpUtility.HtmlDecode(prop.Value);
}
var macroControl = instance as Control;
if (macroControl != null)
{
macroControl.ID = uniqueId;
return macroControl;
}
}
return null;
}
}
}

View File

@@ -17,6 +17,7 @@ namespace Umbraco.Core.ObjectResolution
{
private IEnumerable<TResolved> _applicationInstances = null;
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
private readonly string _httpContextKey;
private readonly List<Type> _instanceTypes = new List<Type>();
private int _defaultPluginWeight = 10;
@@ -41,6 +42,8 @@ namespace Umbraco.Core.ObjectResolution
}
LifetimeScope = scope;
if (scope == ObjectLifetimeScope.HttpRequest)
_httpContextKey = this.GetType().FullName;
_instanceTypes = new List<Type>();
}
@@ -56,6 +59,7 @@ namespace Umbraco.Core.ObjectResolution
if (httpContext == null)
throw new ArgumentNullException("httpContext");
LifetimeScope = ObjectLifetimeScope.HttpRequest;
_httpContextKey = this.GetType().FullName;
CurrentHttpContext = httpContext;
_instanceTypes = new List<Type>();
}
@@ -155,10 +159,10 @@ namespace Umbraco.Core.ObjectResolution
protected IEnumerable<TResolved> Values
{
get
{
// cannot return values unless resolution is frozen, or we can
if (!CanResolveBeforeFrozen && !Resolution.IsFrozen)
throw new InvalidOperationException("Values cannot be returned until resolution is frozen");
{
// ensure we can
if (!CanResolveBeforeFrozen)
Resolution.EnsureIsFrozen();
// note: we apply .ToArray() to the output of CreateInstance() because that is an IEnumerable that
// comes from the PluginManager we want to be _sure_ that it's not a Linq of some sort, but the
@@ -168,16 +172,15 @@ namespace Umbraco.Core.ObjectResolution
{
case ObjectLifetimeScope.HttpRequest:
// create new instances per HttpContext
var key = this.GetType().FullName; // use full type name as key
using (var l = new UpgradeableReadLock(_lock))
{
// create if not already there
if (CurrentHttpContext.Items[key] == null)
if (CurrentHttpContext.Items[_httpContextKey] == null)
{
l.UpgradeToWriteLock();
CurrentHttpContext.Items[key] = CreateInstances().ToArray();
CurrentHttpContext.Items[_httpContextKey] = CreateInstances().ToArray();
}
return (List<TResolved>)CurrentHttpContext.Items[key];
return (List<TResolved>)CurrentHttpContext.Items[_httpContextKey];
}
case ObjectLifetimeScope.Application:
@@ -210,11 +213,13 @@ namespace Umbraco.Core.ObjectResolution
return PluginManager.Current.CreateInstances<TResolved>(InstanceTypes);
}
#region Types collection manipulation
/// <summary>
/// Ensures that a type is a valid type for the resolver.
/// </summary>
/// <param name="value">The type to test.</param>
/// <exception cref="InvalidOperationException"> the type is not a valid type for the resolver.</exception>
/// <exception cref="InvalidOperationException">the type is not a valid type for the resolver.</exception>
protected void EnsureCorrectType(Type value)
{
if (!TypeHelper.IsTypeAssignableFrom<TResolved>(value))
@@ -222,8 +227,6 @@ namespace Umbraco.Core.ObjectResolution
"Type {0} is not an acceptable type for resolver {1}.", value.FullName, this.GetType().FullName));
}
#region Types collection manipulation
/// <summary>
/// Removes a type.
/// </summary>
@@ -233,8 +236,8 @@ namespace Umbraco.Core.ObjectResolution
public virtual void RemoveType(Type value)
{
EnsureRemoveSupport();
EnsureResolutionNotFrozen();
using (Resolution.Configuration)
using (var l = new UpgradeableReadLock(_lock))
{
EnsureCorrectType(value);
@@ -265,8 +268,8 @@ namespace Umbraco.Core.ObjectResolution
protected void AddTypes(IEnumerable<Type> types)
{
EnsureAddSupport();
EnsureResolutionNotFrozen();
using (Resolution.Configuration)
using (new WriteLock(_lock))
{
foreach(var t in types)
@@ -292,8 +295,8 @@ namespace Umbraco.Core.ObjectResolution
public virtual void AddType(Type value)
{
EnsureAddSupport();
EnsureResolutionNotFrozen();
using (Resolution.Configuration)
using (var l = new UpgradeableReadLock(_lock))
{
EnsureCorrectType(value);
@@ -327,8 +330,8 @@ namespace Umbraco.Core.ObjectResolution
public virtual void Clear()
{
EnsureClearSupport();
EnsureResolutionNotFrozen();
using (Resolution.Configuration)
using (new WriteLock(_lock))
{
_instanceTypes.Clear();
@@ -346,8 +349,8 @@ namespace Umbraco.Core.ObjectResolution
public virtual void InsertType(int index, Type value)
{
EnsureInsertSupport();
EnsureResolutionNotFrozen();
using (Resolution.Configuration)
using (var l = new UpgradeableReadLock(_lock))
{
EnsureCorrectType(value);
@@ -373,6 +376,7 @@ namespace Umbraco.Core.ObjectResolution
InsertType(index, typeof(T));
}
/// <summary>
/// Inserts a type before a specified, already existing type.
/// </summary>
/// <param name="existingType">The existing type before which to insert.</param>
@@ -383,8 +387,8 @@ namespace Umbraco.Core.ObjectResolution
public virtual void InsertTypeBefore(Type existingType, Type value)
{
EnsureInsertSupport();
EnsureResolutionNotFrozen();
using (Resolution.Configuration)
using (var l = new UpgradeableReadLock(_lock))
{
EnsureCorrectType(existingType);
@@ -452,15 +456,6 @@ namespace Umbraco.Core.ObjectResolution
{
return new WriteLock(_lock);
}
/// <summary>
/// Throws an exception if resolution is frozen
/// </summary>
protected void EnsureResolutionNotFrozen()
{
if (Resolution.IsFrozen)
throw new InvalidOperationException("The type list cannot be modified after resolution has been frozen");
}
/// <summary>
/// Throws an exception if this does not support Remove

View File

@@ -1,4 +1,5 @@
using System;
using System.Threading;
namespace Umbraco.Core.ObjectResolution
{
@@ -6,12 +7,12 @@ namespace Umbraco.Core.ObjectResolution
/// Represents the status of objects resolution.
/// </summary>
/// <remarks>
/// <para>Objects resolution can be frozen ie nothing can change anymore.</para>
/// <para>Nothing in resolution is thread-safe, because everything should take place when the application is starting.</para>
/// <para>Before resolution is frozen it is possible to access its configuration, but not to get values.</para>
/// <para>Once resolution is frozen, it is not possible to access its configuration anymore, but it is possible to get values.</para>
/// </remarks>
internal class Resolution
internal static class Resolution
{
// NOTE : must clarify freezing... SingleObjectResolverBase does not honor it...
private static readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
/// <summary>
/// Occurs when resolution is frozen.
@@ -22,17 +23,30 @@ namespace Umbraco.Core.ObjectResolution
/// <summary>
/// Gets or sets a value indicating whether resolution of objects is frozen.
/// </summary>
/// <remarks>The internal setter is to be used in unit tests.</remarks>
public static bool IsFrozen { get; internal set; }
public static bool IsFrozen { get; private set; }
public static void EnsureIsFrozen()
{
if (!IsFrozen)
throw new Exception("Resolution is not frozen, it is not yet possible to get values from it.");
}
/// <summary>
/// Ensures that resolution is not frozen, else throws.
/// Returns a disposable object that represents safe access to unfrozen resolution configuration.
/// </summary>
/// <exception cref="InvalidOperationException">resolution is frozen.</exception>
public static void EnsureNotFrozen()
/// <remarks>Should be used in a <c>using(Resolution.Configuration) { ... }</c> mode.</remarks>
public static IDisposable Configuration
{
if (Resolution.IsFrozen)
throw new InvalidOperationException("Resolution is frozen. It is not possible to modify resolvers once resolution is frozen.");
get
{
IDisposable l = new WriteLock(_lock);
if (Resolution.IsFrozen)
{
l.Dispose();
throw new InvalidOperationException("Resolution is frozen, it is not possible to configure it anymore.");
}
return l;
}
}
/// <summary>
@@ -48,5 +62,14 @@ namespace Umbraco.Core.ObjectResolution
if (Frozen != null)
Frozen(null, null);
}
/// <summary>
/// Unfreezes resolution.
/// </summary>
/// <remarks>To be used in unit tests.</remarks>
internal static void Unfreeze()
{
IsFrozen = false;
}
}
}

View File

@@ -63,7 +63,10 @@ namespace Umbraco.Core.ObjectResolution
/// <remarks>To be used in unit tests.</remarks>
internal static void Reset()
{
_resolver = null;
using (new WriteLock(ResolversLock))
{
_resolver = null;
}
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Threading;
namespace Umbraco.Core.ObjectResolution
{
@@ -14,10 +15,9 @@ namespace Umbraco.Core.ObjectResolution
where TResolved : class
where TResolver : class
{
TResolved _resolved;
readonly bool _canBeNull;
// NOTE - we're not freezing resolution here so it is potentially possible to change the instance at any time?
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
private readonly bool _canBeNull;
private TResolved _value;
#region Constructors
@@ -39,7 +39,7 @@ namespace Umbraco.Core.ObjectResolution
protected SingleObjectResolverBase(TResolved value)
: this(false)
{
_resolved = value;
_value = value;
}
/// <summary>
@@ -63,7 +63,7 @@ namespace Umbraco.Core.ObjectResolution
/// otherwise an exception will be thrown when reading it.</remarks>
protected SingleObjectResolverBase(TResolved value, bool canBeNull)
{
_resolved = value;
_value = value;
_canBeNull = canBeNull;
}
@@ -82,7 +82,7 @@ namespace Umbraco.Core.ObjectResolution
/// </summary>
public bool HasValue
{
get { return _resolved != null; }
get { return _value != null; }
}
/// <summary>
@@ -90,21 +90,34 @@ namespace Umbraco.Core.ObjectResolution
/// </summary>
/// <remarks></remarks>
/// <exception cref="ArgumentNullException">value is set to null, but cannot be null (<c>CanBeNull</c> is <c>false</c>).</exception>
/// <exception cref="InvalidOperationException">value is read and is null, but cannot be null (<c>CanBeNull</c> is <c>false</c>).</exception>
/// <exception cref="InvalidOperationException">value is read and is null, but cannot be null (<c>CanBeNull</c> is <c>false</c>),
/// or value is set (read) and resolution is (not) frozen.</exception>
protected TResolved Value
{
get
{
if (!_canBeNull && _resolved == null)
throw new InvalidOperationException("");
return _resolved;
Resolution.EnsureIsFrozen();
using (new ReadLock(_lock))
{
if (!_canBeNull && _value == null)
throw new InvalidOperationException(string.Format(
"Resolver {0} requires a value, and none was supplied.", this.GetType().FullName));
return _value;
}
}
set
{
if (!_canBeNull && value == null)
throw new ArgumentNullException("value");
_resolved = value;
using (Resolution.Configuration)
using (var l = new UpgradeableReadLock(_lock))
{
if (!_canBeNull && value == null)
throw new ArgumentNullException("value");
l.UpgradeToWriteLock();
_value = value;
}
}
}
}