Changed PluginResolverType to PluginManager. Reinstated Resolution and ResolverBase<T> since

this gives us more control over all Resolvers and streamlines them. Created IBootManager, CoreBootManager and WebBootManager
to handle the application initialization including the creation of Resolvers. This means that if people are using the dlls outside
of the web app, they can run the boot strappers to initialize everything.
This commit is contained in:
shannon@ShandemVaio
2012-08-01 22:06:15 +06:00
parent dff3905e85
commit 646e96ab15
52 changed files with 808 additions and 243 deletions

View File

@@ -5,15 +5,16 @@ using System.Web;
namespace Umbraco.Core.Resolving
{
internal abstract class ManyObjectsResolverBase<TResolved>
where TResolved : class
internal abstract class ManyObjectsResolverBase<TResolver, TResolved> : ResolverBase<TResolver>
where TResolved : class
where TResolver : class
{
private List<TResolved> _applicationInstances = null;
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ManyObjectsResolverBase{TResolved}"/> class with an empty list of objects.
/// Initializes a new instance of the <see cref="ManyObjectsResolverBase{TResolver, TResolved}"/> class with an empty list of objects.
/// </summary>
/// <param name="scope">The lifetime scope of instantiated objects, default is per Application</param>
protected ManyObjectsResolverBase(ObjectLifetimeScope scope = ObjectLifetimeScope.Application)
@@ -32,7 +33,7 @@ namespace Umbraco.Core.Resolving
}
/// <summary>
/// Initializes a new instance of the <see cref="ManyObjectsResolverBase{TResolved}"/> class with an empty list of objects.
/// Initializes a new instance of the <see cref="ManyObjectsResolverBase{TResolver, TResolved}"/> class with an empty list of objects.
/// with creation of objects based on an HttpRequest lifetime scope.
/// </summary>
/// <param name="httpContext"></param>
@@ -44,7 +45,7 @@ namespace Umbraco.Core.Resolving
}
/// <summary>
/// Initializes a new instance of the <see cref="ManyObjectsResolverBase{TResolved}"/> class with an initial list of objects.
/// Initializes a new instance of the <see cref="ManyObjectsResolverBase{TResolver, TResolved}"/> class with an initial list of objects.
/// </summary>
/// <param name="value">The list of objects.</param>
/// <param name="scope">If set to true will resolve singleton objects which will be created once for the lifetime of the application</param>
@@ -55,7 +56,7 @@ namespace Umbraco.Core.Resolving
}
/// <summary>
/// Initializes a new instance of the <see cref="ManyObjectsResolverBase{TResolved}"/> class with an initial list of objects
/// Initializes a new instance of the <see cref="ManyObjectsResolverBase{TResolver, TResolved}"/> class with an initial list of objects
/// with creation of objects based on an HttpRequest lifetime scope.
/// </summary>
/// <param name="httpContext"></param>
@@ -90,6 +91,10 @@ namespace Umbraco.Core.Resolving
{
get
{
//We cannot return values unless resolution is locked
if (!Resolution.IsFrozen)
throw new InvalidOperationException("Values cannot be returned until Resolution is frozen");
switch (LifetimeScope)
{
case ObjectLifetimeScope.HttpRequest:
@@ -103,7 +108,7 @@ namespace Umbraco.Core.Resolving
l.UpgradeToWriteLock();
//add the items to the context items (based on full type name)
CurrentHttpContext.Items[this.GetType().FullName] = new List<TResolved>(
PluginTypeResolver.Current.CreateInstances<TResolved>(InstanceTypes));
PluginManager.Current.CreateInstances<TResolved>(InstanceTypes));
}
return (List<TResolved>)CurrentHttpContext.Items[this.GetType().FullName];
}
@@ -115,14 +120,14 @@ namespace Umbraco.Core.Resolving
{
l.UpgradeToWriteLock();
_applicationInstances = new List<TResolved>(
PluginTypeResolver.Current.CreateInstances<TResolved>(InstanceTypes));
PluginManager.Current.CreateInstances<TResolved>(InstanceTypes));
}
return _applicationInstances;
}
case ObjectLifetimeScope.Transient:
default:
//create new instances each time
return PluginTypeResolver.Current.CreateInstances<TResolved>(InstanceTypes);
return PluginManager.Current.CreateInstances<TResolved>(InstanceTypes);
}
}
}

View File

@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Umbraco.Core.Resolving
{
// notes: nothing in Resolving is thread-safe because everything should happen when the app is starting
internal class Resolution
{
public static event EventHandler Frozen;
public static bool IsFrozen { get; private set; }
public static void EnsureNotFrozen()
{
if (Resolution.IsFrozen)
throw new InvalidOperationException("Resolution is frozen. It is not possible to modify resolvers once resolution is frozen.");
}
public static void Freeze()
{
if (Resolution.IsFrozen)
throw new InvalidOperationException("Resolution is frozen. It is not possible to freeze it again.");
IsFrozen = true;
if (Frozen != null)
Frozen(null, null);
}
}
}

View File

@@ -0,0 +1,44 @@
using System;
using System.Threading;
namespace Umbraco.Core.Resolving
{
/// <summary>
/// base class for resolvers which declare a singleton accessor
/// </summary>
/// <typeparam name="TResolver"></typeparam>
internal abstract class ResolverBase<TResolver>
where TResolver : class
{
static TResolver _resolver;
//TODO: This is not correct, this will be the same lock for all ResolverBase classes!!
// this will work i suppose but not really ideal
static readonly ReaderWriterLockSlim ResolversLock = new ReaderWriterLockSlim();
public static TResolver Current
{
get
{
using (new ReadLock(ResolversLock))
{
if (_resolver == null)
throw new InvalidOperationException("Current has not been initialized. You must initialize Current before trying to read it.");
return _resolver;
}
}
set
{
using (new WriteLock(ResolversLock))
{
if (value == null)
throw new ArgumentNullException("value");
if (_resolver != null)
throw new InvalidOperationException("Current has already been initialized. It is not possible to re-initialize Current once it has been initialized.");
_resolver = value;
}
}
}
}
}

View File

@@ -6,12 +6,14 @@ namespace Umbraco.Core.Resolving
/// A Resolver to return and set a Single registered object.
/// </summary>
/// <typeparam name="TResolved"></typeparam>
/// <typeparam name="TResolver"> </typeparam>
/// <remarks>
/// Used for 'singly' registered objects. An example is like the MVC Controller Factory, only one exists application wide and it can
/// be get/set.
/// </remarks>
internal abstract class SingleObjectResolverBase<TResolved>
internal abstract class SingleObjectResolverBase<TResolver, TResolved> : ResolverBase<TResolver>
where TResolved : class
where TResolver : class
{
TResolved _resolved;
readonly bool _canBeNull;