From 7863cd3ab79640e59422a128dec46fe8e57130fa Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 6 Feb 2019 14:11:33 +0100 Subject: [PATCH] Refactor OrderedCollectionBuilderBase AppendAfter to InsertAfter --- .../Composing/OrderedCollectionBuilderBase.cs | 90 +++++++++++++------ .../Composing/CollectionBuildersTests.cs | 36 +++++++- 2 files changed, 98 insertions(+), 28 deletions(-) diff --git a/src/Umbraco.Core/Composing/OrderedCollectionBuilderBase.cs b/src/Umbraco.Core/Composing/OrderedCollectionBuilderBase.cs index 241b84d8d2..2bd4c0d434 100644 --- a/src/Umbraco.Core/Composing/OrderedCollectionBuilderBase.cs +++ b/src/Umbraco.Core/Composing/OrderedCollectionBuilderBase.cs @@ -78,33 +78,6 @@ namespace Umbraco.Core.Composing return This; } - /// - /// Appends a type after another type. - /// - /// The other type. - /// The type to append. - /// The builder. - /// Throws if both types are identical, or if the other type does not already belong to the collection. - public TBuilder AppendAfter() - 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; - } - /// /// Inserts a type into the collection. /// @@ -206,6 +179,69 @@ namespace Umbraco.Core.Composing return This; } + /// + /// Inserts a type after another type. + /// + /// The other type. + /// The type to append. + /// The builder. + /// Throws if both types are identical, or if the other type does not already belong to the collection. + public TBuilder InsertAfter() + 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; + } + + /// + /// Inserts a type after another type. + /// + /// The other type. + /// The type to insert. + /// The builder. + /// Throws if both types are identical, or if the other type does not already belong to the collection. + 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; + } + /// /// Removes a type from the collection. /// diff --git a/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs b/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs index 8ea13fc920..07699f77ed 100644 --- a/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs +++ b/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs @@ -262,6 +262,40 @@ namespace Umbraco.Tests.Composing AssertCollection(col, typeof(Resolved1), typeof(Resolved3), typeof(Resolved2)); } + [Test] + public void CanInsertIntoBuilderAfter() + { + var builder = _composition.WithCollectionBuilder() + .Append() + .Append() + .InsertAfter(); + + Assert.IsTrue(builder.Has()); + Assert.IsTrue(builder.Has()); + Assert.IsTrue(builder.Has()); + + 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() + .Append() + .Append() + .InsertAfter(); + + Assert.IsTrue(builder.Has()); + Assert.IsTrue(builder.Has()); + Assert.IsTrue(builder.Has()); + + 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(); AssertCollection(col1, typeof(Resolved1), typeof(Resolved2));