2021-06-08 14:56:45 -06:00
|
|
|
using System;
|
2016-08-16 10:25:19 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
namespace Umbraco.Cms.Core.Composing
|
2016-08-16 10:25:19 +02:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Implements a lazy 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>
|
2021-07-12 13:34:14 -06:00
|
|
|
/// <remarks>
|
|
|
|
|
/// This type of collection builder is typically used when type scanning is required (i.e. plugins).
|
|
|
|
|
/// </remarks>
|
2016-07-29 10:58:57 +02:00
|
|
|
public abstract class LazyCollectionBuilderBase<TBuilder, TCollection, TItem> : CollectionBuilderBase<TBuilder, TCollection, TItem>
|
2016-08-16 10:25:19 +02:00
|
|
|
where TBuilder : LazyCollectionBuilderBase<TBuilder, TCollection, TItem>
|
2019-01-09 17:39:32 +01:00
|
|
|
where TCollection : class, IBuilderCollection<TItem>
|
2016-08-16 10:25:19 +02:00
|
|
|
{
|
2018-11-28 11:05:41 +01:00
|
|
|
private readonly List<Func<IEnumerable<Type>>> _producers = new List<Func<IEnumerable<Type>>>();
|
2016-08-16 10:25:19 +02:00
|
|
|
private readonly List<Type> _excluded = new List<Type>();
|
|
|
|
|
|
|
|
|
|
protected abstract TBuilder This { get; }
|
|
|
|
|
|
2016-09-23 20:18:25 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Clears all types in the collection.
|
|
|
|
|
/// </summary>
|
2018-11-26 16:54:32 +01:00
|
|
|
/// <returns>The builder.</returns>
|
2016-09-23 20:18:25 +02:00
|
|
|
public TBuilder Clear()
|
|
|
|
|
{
|
|
|
|
|
Configure(types =>
|
|
|
|
|
{
|
|
|
|
|
types.Clear();
|
2018-11-28 11:05:41 +01:00
|
|
|
_producers.Clear();
|
2021-07-12 13:34:14 -06:00
|
|
|
_excluded.Clear();
|
2016-09-23 20:18:25 +02:00
|
|
|
});
|
|
|
|
|
return This;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-16 10:25:19 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a type to the collection.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">The type to add.</typeparam>
|
|
|
|
|
/// <returns>The builder.</returns>
|
|
|
|
|
public TBuilder Add<T>()
|
|
|
|
|
where T : TItem
|
|
|
|
|
{
|
|
|
|
|
Configure(types =>
|
|
|
|
|
{
|
|
|
|
|
var type = typeof(T);
|
2021-07-12 13:34:14 -06:00
|
|
|
if (types.Contains(type) == false)
|
|
|
|
|
types.Add(type);
|
2016-08-16 10:25:19 +02:00
|
|
|
});
|
|
|
|
|
return This;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-23 20:18:25 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a type to the collection.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="type">The type to add.</param>
|
|
|
|
|
/// <returns>The builder.</returns>
|
|
|
|
|
public TBuilder Add(Type type)
|
|
|
|
|
{
|
|
|
|
|
Configure(types =>
|
|
|
|
|
{
|
|
|
|
|
EnsureType(type, "register");
|
2021-07-12 13:34:14 -06:00
|
|
|
if (types.Contains(type) == false)
|
|
|
|
|
types.Add(type);
|
2016-09-23 20:18:25 +02:00
|
|
|
});
|
|
|
|
|
return This;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-16 10:25:19 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a types producer to the collection.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="producer">The types producer.</param>
|
|
|
|
|
/// <returns>The builder.</returns>
|
2016-09-23 20:18:25 +02:00
|
|
|
public TBuilder Add(Func<IEnumerable<Type>> producer)
|
2016-08-16 10:25:19 +02:00
|
|
|
{
|
|
|
|
|
Configure(types =>
|
|
|
|
|
{
|
2018-11-28 11:05:41 +01:00
|
|
|
_producers.Add(producer);
|
2016-08-16 10:25:19 +02:00
|
|
|
});
|
|
|
|
|
return This;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Excludes a type from the collection.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">The type to exclude.</typeparam>
|
|
|
|
|
/// <returns>The builder.</returns>
|
|
|
|
|
public TBuilder Exclude<T>()
|
2016-09-23 20:18:25 +02:00
|
|
|
where T : TItem
|
2016-08-16 10:25:19 +02:00
|
|
|
{
|
|
|
|
|
Configure(types =>
|
|
|
|
|
{
|
|
|
|
|
var type = typeof(T);
|
2021-07-12 13:34:14 -06:00
|
|
|
if (_excluded.Contains(type) == false)
|
|
|
|
|
_excluded.Add(type);
|
2016-08-16 10:25:19 +02:00
|
|
|
});
|
|
|
|
|
return This;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-23 20:18:25 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Excludes a type from the collection.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="type">The type to exclude.</param>
|
|
|
|
|
/// <returns>The builder.</returns>
|
|
|
|
|
public TBuilder Exclude(Type type)
|
|
|
|
|
{
|
|
|
|
|
Configure(types =>
|
|
|
|
|
{
|
|
|
|
|
EnsureType(type, "exclude");
|
2021-07-12 13:34:14 -06:00
|
|
|
if (_excluded.Contains(type) == false)
|
|
|
|
|
_excluded.Add(type);
|
2016-09-23 20:18:25 +02:00
|
|
|
});
|
|
|
|
|
return This;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override IEnumerable<Type> GetRegisteringTypes(IEnumerable<Type> types)
|
2016-08-16 10:25:19 +02:00
|
|
|
{
|
2016-07-29 10:58:57 +02:00
|
|
|
return types
|
2018-11-28 11:05:41 +01:00
|
|
|
.Union(_producers.SelectMany(x => x()))
|
2016-07-29 10:58:57 +02:00
|
|
|
.Distinct()
|
2016-09-23 20:18:25 +02:00
|
|
|
.Select(x => EnsureType(x, "register"))
|
2016-07-29 10:58:57 +02:00
|
|
|
.Except(_excluded);
|
2016-08-16 10:25:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
}
|