Skip to content

ConstructorGuardAttribute

NexusLabs.Needlr.Generators

ConstructorGuardAttribute Class

Requests a constructor guard clause for a field participating in generated constructor generation via GenerateConstructorAttribute.

public sealed class ConstructorGuardAttribute : System.Attribute

Inheritance System.Object 🡒 System.Attribute 🡒 ConstructorGuardAttribute

Example

public partial class TenantService
{
    [ConstructorGuard(ConstructorGuardKind.NotNullOrWhiteSpace)]
    private readonly string _tenantName;
}
public partial class OrderService
{
    [ConstructorGuard(typeof(CollectionNotEmptyGuard))]
    private readonly IReadOnlyCollection<Order> _orders;
}

// Generated call: CollectionNotEmptyGuard.Validate(orders, nameof(orders));
public partial class RetryPolicy
{
    [ConstructorGuard(typeof(NumberGuards), nameof(NumberGuards.ValidatePositive))]
    private readonly int _retryCount;
}

// Generated call: NumberGuards.ValidatePositive(retryCount, nameof(retryCount));
public partial class OrderService
{
    [ConstructorGuard(ConstructorGuardKind.NotNullOrWhiteSpace)]
    [ConstructorGuard(typeof(OrderIdFormatGuard))]
    private readonly string _orderId;
}

// Generated calls, in declaration order:
// global::System.ArgumentException.ThrowIfNullOrWhiteSpace(orderId);
// OrderIdFormatGuard.Validate(orderId, nameof(orderId));

Remarks

Applying this attribute with any kind other than None, or with a custom guard type, is itself a positive trigger: it enables constructor generation with None even if the containing class does not carry GenerateConstructorAttribute. In that case every other eligible field still becomes a constructor parameter, but only fields with their own explicit guard receive one.

A custom guard type must expose an accessible static method compatible with void Validate(T value, string parameterName), where T is compatible with the field's type. The generator emits a direct, fully-qualified call to that method -- never a reflection-based invocation.

This attribute may be applied more than once to the same field to request several guards additively. The class-level default guard (when applicable) is always composed first, followed by every explicit ConstructorGuardAttribute in source declaration order. Identical effective guard calls -- the same built-in ConstructorGuardKind, or the same resolved custom guard type and method -- are emitted only once even if requested more than once.

Constructors

ConstructorGuardAttribute(ConstructorGuardKind) Constructor

Initializes a new instance of the ConstructorGuardAttribute class requesting a built-in guard kind.

public ConstructorGuardAttribute(NexusLabs.Needlr.Generators.ConstructorGuardKind kind);

Parameters

kind ConstructorGuardKind

The built-in guard clause to emit for this field.

ConstructorGuardAttribute(Type) Constructor

Initializes a new instance of the ConstructorGuardAttribute class requesting a custom guard type using the conventional Validate method.

public ConstructorGuardAttribute(System.Type guardType);

Parameters

guardType System.Type

A type exposing an accessible static method named Validate, compatible with void Validate(T value, string parameterName).

ConstructorGuardAttribute(Type, string) Constructor

Initializes a new instance of the ConstructorGuardAttribute class requesting a custom guard type and an explicit validation method name.

public ConstructorGuardAttribute(System.Type guardType, string methodName);

Parameters

guardType System.Type

A type exposing an accessible static method matching methodName, compatible with void Validate(T value, string parameterName).

methodName System.String

The name of the static validation method to call, typically supplied via nameof.

Properties

ConstructorGuardAttribute.GuardType Property

Gets the custom guard type supplied to this attribute, or null when a built-in Kind was supplied instead.

public System.Type? GuardType { get; }

Property Value

System.Type

ConstructorGuardAttribute.Kind Property

Gets the requested built-in guard kind, or None when a custom GuardType was supplied instead.

public NexusLabs.Needlr.Generators.ConstructorGuardKind Kind { get; }

Property Value

ConstructorGuardKind

ConstructorGuardAttribute.MethodName Property

Gets the explicit validation method name supplied to this attribute, or null when the conventional Validate method name applies.

public string? MethodName { get; }

Property Value

System.String