Changes all collections from collection builders to resolve the concrete instances lazily.

This means we don't have to inject Lazy<T> all over the place when dealing with
colleciton builders and circular references since this will automatically just work OOTB.
This in theory should also allocate less instances during startup.
This commit is contained in:
Shannon
2021-07-12 15:28:46 -06:00
parent 461a199d15
commit bc84ffe260
85 changed files with 327 additions and 274 deletions

View File

@@ -1,43 +1,34 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Umbraco.Cms.Core.Composing
{
/// <summary>
/// Provides a base class for builder collections.
/// </summary>
/// <typeparam name="TItem">The type of the items.</typeparam>
public abstract class BuilderCollectionBase<TItem> : IBuilderCollection<TItem>
{
private readonly TItem[] _items;
private readonly LazyReadOnlyCollection<TItem> _items;
/// <summary>
/// Initializes a new instance of the <see cref="BuilderCollectionBase{TItem}"/> with items.
/// </summary>
/// <param name="items">The items.</param>
protected BuilderCollectionBase(IEnumerable<TItem> items)
{
_items = items.ToArray();
}
public BuilderCollectionBase(Func<IEnumerable<TItem>> items) => _items = new LazyReadOnlyCollection<TItem>(items);
/// <inheritdoc />
public int Count => _items.Length;
public int Count => _items.Count;
/// <summary>
/// Gets an enumerator.
/// </summary>
public IEnumerator<TItem> GetEnumerator()
{
return ((IEnumerable<TItem>) _items).GetEnumerator();
}
public IEnumerator<TItem> GetEnumerator() => ((IEnumerable<TItem>)_items).GetEnumerator();
/// <summary>
/// Gets an enumerator.
/// </summary>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}