Files
Umbraco-CMS/src/Umbraco.Core/Resolving/ManyObjectResolverBase.cs

103 lines
2.9 KiB
C#
Raw Normal View History

2012-07-27 05:52:01 +06:00
using System.Collections.Generic;
using System.Threading;
namespace Umbraco.Core.Resolving
{
//NOTE: This class should also support creating instances of the object and managing their lifespan,
// for example, some resolvers would want to return new object each time, per request or per application lifetime.
2012-07-27 05:52:01 +06:00
/// <summary>
2012-07-27 14:06:41 -02:00
/// A Resolver which manages an ordered list of objects.
2012-07-27 05:52:01 +06:00
/// </summary>
2012-07-27 14:06:41 -02:00
/// <typeparam name="TResolver">The type of the resolver.</typeparam>
/// <typeparam name="TResolved">The type of the resolved objects.</typeparam>
2012-07-27 05:52:01 +06:00
/// <remarks>
/// Used to resolve multiple types from a collection. The collection can also be modified at runtime/application startup.
2012-07-27 14:06:41 -02:00
/// An example of this is MVCs ViewEngines collection.
2012-07-27 05:52:01 +06:00
/// </remarks>
internal abstract class ManyObjectResolverBase<TResolver, TResolved> : ResolverBase<TResolver>
2012-07-27 05:52:01 +06:00
where TResolver : class
where TResolved : class
{
readonly List<TResolved> _resolved;
protected readonly ReaderWriterLockSlim Lock = new ReaderWriterLockSlim();
2012-07-27 14:06:41 -02:00
/// <summary>
/// Initializes a new instance of the <see cref="ManyObjectResolverBase{TResolver,TResolved}"/> class with an empty list of objects.
2012-07-27 14:06:41 -02:00
/// </summary>
protected ManyObjectResolverBase()
2012-07-27 05:52:01 +06:00
{
_resolved = new List<TResolved>();
}
2012-07-27 14:06:41 -02:00
/// <summary>
/// Initializes a new instance of the <see cref="ManyObjectResolverBase{TResolver,TResolved}"/> class with an initial list of objects.
2012-07-27 14:06:41 -02:00
/// </summary>
/// <param name="values">The list of objects.</param>
protected ManyObjectResolverBase(IEnumerable<TResolved> value)
2012-07-27 05:52:01 +06:00
{
_resolved = new List<TResolved>(value);
2012-07-27 14:06:41 -02:00
}
2012-07-27 05:52:01 +06:00
2012-07-27 14:06:41 -02:00
/// <summary>
/// Gets the list of objects.
/// </summary>
protected IEnumerable<TResolved> Values
2012-07-27 05:52:01 +06:00
{
get { return _resolved; }
}
2012-07-27 14:06:41 -02:00
/// <summary>
/// Removes an object.
/// </summary>
/// <param name="value">The object to remove.</param>
public void Remove(TResolved value)
2012-07-27 05:52:01 +06:00
{
Resolution.EnsureNotFrozen();
using (new WriteLock(Lock))
{
2012-07-27 14:06:41 -02:00
_resolved.Remove(value);
2012-07-27 05:52:01 +06:00
}
}
2012-07-27 14:06:41 -02:00
/// <summary>
/// Adds an object to the end of the list.
/// </summary>
/// <param name="value">The object to be added.</param>
public void Add(TResolved value)
2012-07-27 05:52:01 +06:00
{
Resolution.EnsureNotFrozen();
using (new WriteLock(Lock))
{
2012-07-27 14:06:41 -02:00
_resolved.Add(value);
}
2012-07-27 05:52:01 +06:00
}
2012-07-27 14:06:41 -02:00
/// <summary>
/// Clears the list.
/// </summary>
2012-07-27 05:52:01 +06:00
public void Clear()
{
Resolution.EnsureNotFrozen();
using (new WriteLock(Lock))
{
_resolved.Clear();
}
2012-07-27 05:52:01 +06:00
}
2012-07-27 14:06:41 -02:00
/// <summary>
/// Inserts an object at the specified index.
/// </summary>
/// <param name="index">The zero-based index at which the object should be inserted.</param>
/// <param name="value">The object to insert.</param>
public void Insert(int index, TResolved value)
2012-07-27 05:52:01 +06:00
{
Resolution.EnsureNotFrozen();
using (new WriteLock(Lock))
{
2012-07-27 14:06:41 -02:00
_resolved.Insert(index, value);
}
2012-07-27 05:52:01 +06:00
}
}
}