< Summary

Information
Class: NexusLabs.Needlr.Generators.GenerateConstructorAttribute
Assembly: NexusLabs.Needlr.Generators.Attributes
File(s): /_work/nick-alienware-needlr-1-1784953859-3e8997/needlr/needlr/src/NexusLabs.Needlr.Generators.Attributes/GenerateConstructorAttribute.cs
Line coverage
83%
Covered lines: 5
Uncovered lines: 1
Coverable lines: 6
Total lines: 109
Line coverage: 83.3%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
.ctor(...)100%11100%
get_Mode()100%210%

File(s)

/_work/nick-alienware-needlr-1-1784953859-3e8997/needlr/needlr/src/NexusLabs.Needlr.Generators.Attributes/GenerateConstructorAttribute.cs

#LineLine coverage
 1using System;
 2
 3namespace NexusLabs.Needlr.Generators;
 4
 5/// <summary>
 6/// Generates a public constructor for a partial class from its eligible private
 7/// instance <see langword="readonly"/> fields, eliminating hand-written constructor,
 8/// field-assignment, and guard-clause boilerplate.
 9/// </summary>
 10/// <remarks>
 11/// <para>
 12/// The generator produces exactly one public constructor whose parameters are derived,
 13/// in declaration order, from every eligible field: a private, instance,
 14/// <see langword="readonly"/> field without an initializer, unless the field carries
 15/// <see cref="ConstructorIgnoreAttribute"/>. Field names using the <c>_camelCase</c>
 16/// convention are mapped to <c>camelCase</c> parameter names.
 17/// </para>
 18/// <para>
 19/// This attribute never introduces optional parameters or overloads on the generated
 20/// constructor itself. Use an explicit hand-written constructor overload, or a
 21/// get-only property with its own initialization logic, when optional construction
 22/// paths are required.
 23/// </para>
 24/// <para>
 25/// The containing class must be declared <see langword="partial"/> and must not
 26/// declare its own instance constructor, because Roslyn source generators can only add
 27/// new members to a partial type -- they cannot rewrite or inject statements into a
 28/// user-authored constructor. The containing type must also not be nested. It may
 29/// derive from <see cref="object"/> directly, or from any base type that itself has an
 30/// accessible (public or protected) parameterless constructor -- including the common
 31/// case of a base type with no explicit constructors at all -- since the generated
 32/// constructor relies on the implicit <c>: base()</c> call. This supports common
 33/// framework base types such as <c>BackgroundService</c>. A base type that requires
 34/// constructor arguments is unsupported: no source is emitted, and an analyzer reports
 35/// the requirement at compile time instead.
 36/// </para>
 37/// <para>
 38/// A field-level positive guard attribute -- a built-in or custom
 39/// <see cref="ConstructorGuardAttribute"/>, or a custom alias attribute defined with
 40/// <see cref="ConstructorGuardDefinitionAttribute"/> -- also enables constructor
 41/// generation with <see cref="ConstructorNullGuardMode.None"/>, even when this
 42/// attribute is not applied to the class.
 43/// </para>
 44/// </remarks>
 45/// <example>
 46/// <code>
 47/// [GenerateConstructor]
 48/// public partial class UserService
 49/// {
 50///     private readonly IRepository _repository;
 51/// }
 52///
 53/// // Generated:
 54/// // public UserService(IRepository repository)
 55/// // {
 56/// //     _repository = repository;
 57/// // }
 58/// </code>
 59/// <code>
 60/// [GenerateConstructor]
 61/// public partial class MyWorker : BackgroundService
 62/// {
 63///     private readonly IRepository _repository;
 64///
 65///     protected override Task ExecuteAsync(CancellationToken stoppingToken)
 66///     {
 67///         return Task.CompletedTask;
 68///     }
 69/// }
 70///
 71/// // BackgroundService has an accessible parameterless constructor, so the generated
 72/// // constructor relies on the implicit base() call:
 73/// // public MyWorker(IRepository repository)
 74/// // {
 75/// //     _repository = repository;
 76/// // }
 77/// </code>
 78/// </example>
 79[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
 80public sealed class GenerateConstructorAttribute : Attribute
 81{
 82    /// <summary>
 83    /// Initializes a new instance of the <see cref="GenerateConstructorAttribute"/>
 84    /// class with <see cref="ConstructorNullGuardMode.None"/>. No automatic null
 85    /// guards are emitted.
 86    /// </summary>
 87    public GenerateConstructorAttribute()
 574088        : this(ConstructorNullGuardMode.None)
 89    {
 574090    }
 91
 92    /// <summary>
 93    /// Initializes a new instance of the <see cref="GenerateConstructorAttribute"/>
 94    /// class with an explicit null-guard mode.
 95    /// </summary>
 96    /// <param name="mode">
 97    /// Controls whether the generator automatically emits null guards for eligible
 98    /// non-nullable reference-type fields.
 99    /// </param>
 7380100    public GenerateConstructorAttribute(ConstructorNullGuardMode mode)
 101    {
 7380102        Mode = mode;
 7380103    }
 104
 105    /// <summary>
 106    /// Gets the configured null-guard mode for the generated constructor.
 107    /// </summary>
 0108    public ConstructorNullGuardMode Mode { get; }
 109}