Files
Umbraco-CMS/src/Umbraco.Core/DependencyInjection/OrderedCollectionBuilderBase.cs

221 lines
8.1 KiB
C#
Raw Normal View History

2016-08-16 10:25:19 +02:00
using System;
2016-08-19 11:41:39 +02:00
using System.Collections.Generic;
2016-08-16 10:25:19 +02:00
using LightInject;
namespace Umbraco.Core.DependencyInjection
{
/// <summary>
/// Implements an ordered collection builder.
/// </summary>
/// <typeparam name="TBuilder">The type of the builder.</typeparam>
/// <typeparam name="TCollection">The type of the collection.</typeparam>
/// <typeparam name="TItem">The type of the items.</typeparam>
2016-07-29 10:58:57 +02:00
public abstract class OrderedCollectionBuilderBase<TBuilder, TCollection, TItem> : CollectionBuilderBase<TBuilder, TCollection, TItem>
2016-08-16 10:25:19 +02:00
where TBuilder : OrderedCollectionBuilderBase<TBuilder, TCollection, TItem>
where TCollection : IBuilderCollection<TItem>
{
protected OrderedCollectionBuilderBase(IServiceContainer container)
: base (container)
{ }
protected abstract TBuilder This { get; }
/// <summary>
/// Appends a type to the collection.
/// </summary>
/// <typeparam name="T">The type to append.</typeparam>
/// <returns>The builder.</returns>
public TBuilder Append<T>()
where T : TItem
{
Configure(types =>
{
var type = typeof (T);
if (types.Contains(type)) types.Remove(type);
types.Add(type);
});
return This;
}
2016-08-19 11:41:39 +02:00
/// <summary>
/// Appends types to the collections.
/// </summary>
/// <param name="types">The types to append.</param>
/// <returns>The builder.</returns>
public TBuilder Append(IEnumerable<Type> types)
{
Configure(list =>
{
foreach (var type in types)
{
2016-08-22 11:40:45 +02:00
// would be detected by CollectionBuilderBase when registering, anyways, but let's fail fast
if (typeof(TItem).IsAssignableFrom(type) == false)
throw new InvalidOperationException($"Cannot register type {type.FullName} as it does not inherit from/implement {typeof(TItem).FullName}.");
2016-08-19 11:41:39 +02:00
if (list.Contains(type)) list.Remove(type);
list.Add(type);
}
});
return This;
2016-08-25 15:09:51 +02:00
}
/// <summary>
/// Appends types to the collections.
/// </summary>
/// <param name="types">The types to append.</param>
/// <returns>The builder.</returns>
public TBuilder Append(Func<IServiceFactory, IEnumerable<Type>> types)
{
Configure(list =>
{
foreach (var type in types(Container))
{
// would be detected by CollectionBuilderBase when registering, anyways, but let's fail fast
if (typeof(TItem).IsAssignableFrom(type) == false)
throw new InvalidOperationException($"Cannot register type {type.FullName} as it does not inherit from/implement {typeof(TItem).FullName}.");
if (list.Contains(type)) list.Remove(type);
list.Add(type);
}
});
return This;
2016-08-19 11:41:39 +02:00
}
2016-08-16 10:25:19 +02:00
/// <summary>
/// Appends a type after another type.
/// </summary>
/// <typeparam name="TAfter">The other type.</typeparam>
/// <typeparam name="T">The type to append.</typeparam>
/// <returns>The builder.</returns>
/// <remarks>Throws if both types are identical, or if the other type does not already belong to the collection.</remarks>
public TBuilder AppendAfter<TAfter, T>()
where TAfter : TItem
where T : TItem
{
Configure(types =>
{
var typeAfter = typeof (TAfter);
var type = typeof(T);
if (typeAfter == type) throw new InvalidOperationException();
var index = types.IndexOf(typeAfter);
if (index < 0) throw new InvalidOperationException();
if (types.Contains(type)) types.Remove(type);
index = types.IndexOf(typeAfter); // in case removing type changed index
types.Insert(index + 1, type);
});
return This;
}
/// <summary>
/// Inserts a type into the collection.
/// </summary>
/// <typeparam name="T">The type to insert.</typeparam>
2016-08-22 11:40:45 +02:00
/// <param name="index">The optional index.</param>
2016-08-16 10:25:19 +02:00
/// <returns>The builder.</returns>
2016-08-22 11:40:45 +02:00
/// <remarks>Throws if the index is out of range.</remarks>
public TBuilder Insert<T>(int index = 0)
2016-08-16 10:25:19 +02:00
where T : TItem
{
Configure(types =>
{
var type = typeof (T);
if (types.Contains(type)) types.Remove(type);
2016-08-22 11:40:45 +02:00
types.Insert(index, type);
2016-08-16 10:25:19 +02:00
});
return This;
}
/// <summary>
/// Inserts a type before another type.
/// </summary>
/// <typeparam name="TBefore">The other type.</typeparam>
/// <typeparam name="T">The type to insert.</typeparam>
/// <returns>The builder.</returns>
/// <remarks>Throws if both types are identical, or if the other type does not already belong to the collection.</remarks>
public TBuilder InsertBefore<TBefore, T>()
where TBefore : TItem
where T : TItem
{
Configure(types =>
{
var typeBefore = typeof(TBefore);
var type = typeof(T);
if (typeBefore == type) throw new InvalidOperationException();
var index = types.IndexOf(typeBefore);
if (index < 0) throw new InvalidOperationException();
if (types.Contains(type)) types.Remove(type);
index = types.IndexOf(typeBefore); // in case removing type changed index
types.Insert(index, type);
});
return This;
}
/// <summary>
/// Removes a type from the collection.
/// </summary>
/// <typeparam name="T">The type to remove.</typeparam>
/// <returns>The builder.</returns>
public TBuilder Remove<T>()
where T : TItem
{
Configure(types =>
{
var type = typeof (T);
if (types.Contains(type)) types.Remove(type);
});
return This;
}
/// <summary>
/// Replaces a type in the collection.
/// </summary>
/// <typeparam name="TReplaced">The type to replace.</typeparam>
/// <typeparam name="T">The type to insert.</typeparam>
/// <returns>The builder.</returns>
/// <remarks>Throws if the type to replace does not already belong to the collection.</remarks>
public TBuilder Replace<TReplaced, T>()
where TReplaced : TItem
where T : TItem
{
Configure(types =>
{
var typeReplaced = typeof(TReplaced);
var type = typeof(T);
if (typeReplaced == type) return;
var index = types.IndexOf(typeReplaced);
if (index < 0) throw new InvalidOperationException();
if (types.Contains(type)) types.Remove(type);
index = types.IndexOf(typeReplaced); // in case removing type changed index
types.Insert(index, type);
types.Remove(typeReplaced);
});
return This;
}
2016-08-22 11:40:45 +02:00
/// <summary>
/// Gets a value indicating whether the collection contains a type.
/// </summary>
/// <typeparam name="T">The type to look for.</typeparam>
/// <returns>A value indicating whether the collection contains the type.</returns>
public bool Has<T>()
where T : TItem
{
return HasBase<T>();
}
/// <summary>
/// Clears all types in the collection.
/// </summary>
/// <returns>The buidler.</returns>
public TBuilder Clear()
{
Configure(types => types.Clear());
return This;
}
2016-08-16 10:25:19 +02:00
}
}