2019-01-03 21:00:28 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using Umbraco.Core.Logging;
|
|
|
|
|
|
|
2019-02-14 09:15:47 +01:00
|
|
|
|
namespace Umbraco.Core.Composing
|
2019-01-03 21:00:28 +01:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents the collection of <see cref="IComponent"/> implementations.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class ComponentCollection : BuilderCollectionBase<IComponent>
|
|
|
|
|
|
{
|
|
|
|
|
|
private const int LogThresholdMilliseconds = 100;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly IProfilingLogger _logger;
|
|
|
|
|
|
|
|
|
|
|
|
public ComponentCollection(IEnumerable<IComponent> items, IProfilingLogger logger)
|
|
|
|
|
|
: base(items)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-07 09:30:47 +01:00
|
|
|
|
public void Initialize()
|
|
|
|
|
|
{
|
|
|
|
|
|
using (_logger.DebugDuration<ComponentCollection>($"Initializing. (log components when >{LogThresholdMilliseconds}ms)", "Initialized."))
|
|
|
|
|
|
{
|
2019-02-14 10:39:27 +01:00
|
|
|
|
foreach (var component in this)
|
2019-01-07 09:30:47 +01:00
|
|
|
|
{
|
|
|
|
|
|
var componentType = component.GetType();
|
|
|
|
|
|
using (_logger.DebugDuration<ComponentCollection>($"Initializing {componentType.FullName}.", $"Initialized {componentType.FullName}.", thresholdMilliseconds: LogThresholdMilliseconds))
|
|
|
|
|
|
{
|
|
|
|
|
|
component.Initialize();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-03 21:00:28 +01:00
|
|
|
|
public void Terminate()
|
|
|
|
|
|
{
|
|
|
|
|
|
using (_logger.DebugDuration<ComponentCollection>($"Terminating. (log components when >{LogThresholdMilliseconds}ms)", "Terminated."))
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var component in this.Reverse()) // terminate components in reverse order
|
|
|
|
|
|
{
|
|
|
|
|
|
var componentType = component.GetType();
|
|
|
|
|
|
using (_logger.DebugDuration<ComponentCollection>($"Terminating {componentType.FullName}.", $"Terminated {componentType.FullName}.", thresholdMilliseconds: LogThresholdMilliseconds))
|
|
|
|
|
|
{
|
2019-01-07 09:30:47 +01:00
|
|
|
|
component.Terminate();
|
2019-01-03 21:00:28 +01:00
|
|
|
|
component.DisposeIfDisposable();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|