using System;
namespace Umbraco.Core.Components
{
///
/// Indicates that a component requires another component.
///
///
/// This attribute is *not* inherited. This means that a component class inheriting from
/// another component class does *not* inherit its requirements. However, the bootloader checks
/// the *interfaces* of every component for their requirements, so requirements declared on
/// interfaces are inherited by every component class implementing the interface.
/// When targetting a class, indicates a dependency on the component which must be enabled,
/// unless the requirement has explicitely been declared as weak (and then, only if the component
/// is enabled).
/// When targetting an interface, indicates a dependency on enabled components implementing
/// the interface. It could be no component at all, unless the requirement has explicitely been
/// declared as strong (and at least one component must be enabled).
///
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = true, Inherited = false)]
public class RequireComponentAttribute : Attribute
{
///
/// Initializes a new instance of the class.
///
/// The type of the required component.
public RequireComponentAttribute(Type requiredType)
{
if (typeof(IUmbracoComponent).IsAssignableFrom(requiredType) == false)
throw new ArgumentException($"Type {requiredType.FullName} is invalid here because it does not implement {typeof(IUmbracoComponent).FullName}.");
RequiredType = requiredType;
}
///
/// Initializes a new instance of the class.
///
/// The type of the required component.
/// A value indicating whether the requirement is weak.
public RequireComponentAttribute(Type requiredType, bool weak)
: this(requiredType)
{
Weak = weak;
}
///
/// Gets the required type.
///
public Type RequiredType { get; }
///
/// Gets a value indicating whether the requirement is weak.
///
/// Returns true if the requirement is weak (requires the other component if it
/// is enabled), false if the requirement is strong (requires the other component to be
/// enabled), and null if unspecified, in which case it is strong for classes and weak for
/// interfaces.
public bool? Weak { get; }
}
}