2012-08-01 22:06:15 +06:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
2012-08-10 13:18:13 +06:00
|
|
|
|
namespace Umbraco.Core.ObjectResolution
|
2012-08-01 22:06:15 +06:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2013-01-16 13:10:34 -01:00
|
|
|
|
/// The base class for all resolvers.
|
2012-08-01 22:06:15 +06:00
|
|
|
|
/// </summary>
|
2013-01-16 13:10:34 -01:00
|
|
|
|
/// <typeparam name="TResolver">The type of the concrete resolver class.</typeparam>
|
|
|
|
|
|
/// <remarks>Provides singleton management to all resolvers.</remarks>
|
|
|
|
|
|
public abstract class ResolverBase<TResolver>
|
2012-08-01 22:06:15 +06:00
|
|
|
|
where TResolver : class
|
|
|
|
|
|
{
|
|
|
|
|
|
static TResolver _resolver;
|
2012-08-01 22:46:13 +06:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-01-16 13:10:34 -01:00
|
|
|
|
/// The lock for the singleton.
|
2012-08-01 22:46:13 +06:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// Though resharper says this is in error, it is actually correct. We want a different lock object for each generic type.
|
|
|
|
|
|
/// See this for details: http://confluence.jetbrains.net/display/ReSharper/Static+field+in+generic+type
|
|
|
|
|
|
/// </remarks>
|
2012-08-01 22:06:15 +06:00
|
|
|
|
static readonly ReaderWriterLockSlim ResolversLock = new ReaderWriterLockSlim();
|
|
|
|
|
|
|
2013-01-16 13:10:34 -01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the resolver singleton instance.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>The value can be set only once, and cannot be read before it has been set.</remarks>
|
|
|
|
|
|
/// <exception cref="InvalidOperationException">value is read before it has been set, or value is set again once it has already been set.</exception>
|
|
|
|
|
|
/// <exception cref="ArgumentNullException">value is <c>null</c>.</exception>
|
2012-08-01 22:06:15 +06:00
|
|
|
|
public static TResolver Current
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
using (new ReadLock(ResolversLock))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_resolver == null)
|
2013-01-16 13:10:34 -01:00
|
|
|
|
throw new InvalidOperationException(string.Format(
|
|
|
|
|
|
"Current has not been initialized on {0}. You must initialize Current before trying to read it.",
|
|
|
|
|
|
typeof(TResolver).FullName));
|
2012-08-01 22:06:15 +06:00
|
|
|
|
return _resolver;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
using (new WriteLock(ResolversLock))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (value == null)
|
|
|
|
|
|
throw new ArgumentNullException("value");
|
|
|
|
|
|
if (_resolver != null)
|
2013-01-16 13:10:34 -01:00
|
|
|
|
throw new InvalidOperationException(string.Format(
|
|
|
|
|
|
"Current has already been initialized on {0}. It is not possible to re-initialize Current once it has been initialized.",
|
|
|
|
|
|
typeof(TResolver).FullName));
|
2012-08-01 22:06:15 +06:00
|
|
|
|
_resolver = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-08-01 22:46:13 +06:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-01-16 13:10:34 -01:00
|
|
|
|
/// Resets the resolver singleton instance to null.
|
2012-08-01 22:46:13 +06:00
|
|
|
|
/// </summary>
|
2013-01-16 13:10:34 -01:00
|
|
|
|
/// <remarks>To be used in unit tests.</remarks>
|
2012-08-01 22:46:13 +06:00
|
|
|
|
internal static void Reset()
|
|
|
|
|
|
{
|
2013-01-16 13:31:04 -01:00
|
|
|
|
using (new WriteLock(ResolversLock))
|
|
|
|
|
|
{
|
|
|
|
|
|
_resolver = null;
|
|
|
|
|
|
}
|
2012-08-01 22:46:13 +06:00
|
|
|
|
}
|
2012-08-01 22:06:15 +06:00
|
|
|
|
}
|
|
|
|
|
|
}
|