Files
Umbraco-CMS/src/Umbraco.Core/Composing/BuilderCollectionBase.cs
Shannon bc84ffe260 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.
2021-07-12 15:28:46 -06:00

35 lines
1.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
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 LazyReadOnlyCollection<TItem> _items;
/// Initializes a new instance of the <see cref="BuilderCollectionBase{TItem}"/> with items.
/// </summary>
/// <param name="items">The items.</param>
public BuilderCollectionBase(Func<IEnumerable<TItem>> items) => _items = new LazyReadOnlyCollection<TItem>(items);
/// <inheritdoc />
public int Count => _items.Count;
/// <summary>
/// Gets an enumerator.
/// </summary>
public IEnumerator<TItem> GetEnumerator() => ((IEnumerable<TItem>)_items).GetEnumerator();
/// <summary>
/// Gets an enumerator.
/// </summary>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}