Refactor OrderedCollectionBuilderBase AppendAfter to InsertAfter

This commit is contained in:
Stephan
2019-02-06 14:11:33 +01:00
parent a4a437a746
commit 7863cd3ab7
2 changed files with 98 additions and 28 deletions

View File

@@ -78,33 +78,6 @@ namespace Umbraco.Core.Composing
return This;
}
/// <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>
@@ -206,6 +179,69 @@ namespace Umbraco.Core.Composing
return This;
}
/// <summary>
/// Inserts 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 InsertAfter<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
index += 1; // insert here
if (index == types.Count)
types.Add(type);
else
types.Insert(index, type);
});
return This;
}
/// <summary>
/// Inserts a type after another type.
/// </summary>
/// <param name="typeAfter">The other type.</param>
/// <param name="type">The type to insert.</param>
/// <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 InsertAfter(Type typeAfter, Type type)
{
Configure(types =>
{
EnsureType(typeAfter, "find");
EnsureType(type, "register");
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
index += 1; // insert here
if (index == types.Count)
types.Add(type);
else
types.Insert(index, type);
});
return This;
}
/// <summary>
/// Removes a type from the collection.
/// </summary>

View File

@@ -262,6 +262,40 @@ namespace Umbraco.Tests.Composing
AssertCollection(col, typeof(Resolved1), typeof(Resolved3), typeof(Resolved2));
}
[Test]
public void CanInsertIntoBuilderAfter()
{
var builder = _composition.WithCollectionBuilder<TestCollectionBuilder>()
.Append<Resolved1>()
.Append<Resolved2>()
.InsertAfter<Resolved1, Resolved3>();
Assert.IsTrue(builder.Has<Resolved1>());
Assert.IsTrue(builder.Has<Resolved2>());
Assert.IsTrue(builder.Has<Resolved3>());
var factory = _composition.CreateFactory();
var col = builder.CreateCollection(factory);
AssertCollection(col, typeof(Resolved1), typeof(Resolved3), typeof(Resolved2));
}
[Test]
public void CanInsertIntoBuilderAfterLast()
{
var builder = _composition.WithCollectionBuilder<TestCollectionBuilder>()
.Append<Resolved1>()
.Append<Resolved2>()
.InsertAfter<Resolved2, Resolved3>();
Assert.IsTrue(builder.Has<Resolved1>());
Assert.IsTrue(builder.Has<Resolved2>());
Assert.IsTrue(builder.Has<Resolved3>());
var factory = _composition.CreateFactory();
var col = builder.CreateCollection(factory);
AssertCollection(col, typeof(Resolved1), typeof(Resolved2), typeof(Resolved3));
}
[Test]
public void CannotInsertIntoBuilderBeforeOnceCollectionIsCreated()
{
@@ -314,7 +348,7 @@ namespace Umbraco.Tests.Composing
var factory = _composition.CreateFactory();
using (factory.BeginScope())
{
{
var col1 = factory.GetInstance<TestCollection>();
AssertCollection(col1, typeof(Resolved1), typeof(Resolved2));