Files
Umbraco-CMS/src/Umbraco.Core/Composing/LazyCollectionBuilderBase.cs

130 lines
4.0 KiB
C#
Raw Normal View History

using System;
2016-08-16 10:25:19 +02:00
using System.Collections.Generic;
using System.Linq;
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>
/// <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>
/// <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();
_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);
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");
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);
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");
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
}