< Summary

Information
Class: NexusLabs.Needlr.DeferToContainerAttribute
Assembly: NexusLabs.Needlr
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr/DeferToContainerAttribute.cs
Line coverage
100%
Covered lines: 4
Uncovered lines: 0
Coverable lines: 4
Total lines: 65
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%22100%
get_ConstructorParameterTypes()100%11100%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr/DeferToContainerAttribute.cs

#LineLine coverage
 1namespace NexusLabs.Needlr;
 2
 3/// <summary>
 4/// Specifies constructor parameter types for a partial class whose constructor
 5/// will be generated by another source generator.
 6/// </summary>
 7/// <remarks>
 8/// <para>
 9/// Use this attribute when your partial class receives a constructor from another
 10/// source generator (e.g., a code generator that adds a primary constructor based
 11/// on an attribute). Since Needlr's source generator cannot see constructors added
 12/// by other generators, you must explicitly declare the expected constructor
 13/// parameter types.
 14/// </para>
 15/// <para>
 16/// Needlr will generate a factory that calls the constructor with the specified
 17/// parameter types, resolving each dependency from the service provider.
 18/// </para>
 19/// <para>
 20/// If the declared parameter types don't match the actual generated constructor,
 21/// the build will fail with a compile error.
 22/// </para>
 23/// </remarks>
 24/// <example>
 25/// <code>
 26/// // Your partial class - another generator will add a primary constructor
 27/// [DeferToContainer(typeof(IRepository), typeof(ILogger&lt;MyService&gt;))]
 28/// [GeneratedService]  // This triggers another source generator
 29/// public partial class MyService { }
 30///
 31/// // The other generator produces:
 32/// // public partial class MyService(IRepository repository, ILogger&lt;MyService&gt; logger) { }
 33///
 34/// // Needlr generates factory based on your [DeferToContainer] declaration:
 35/// // sp => new MyService(
 36/// //     sp.GetRequiredService&lt;IRepository&gt;(),
 37/// //     sp.GetRequiredService&lt;ILogger&lt;MyService&gt;&gt;())
 38/// </code>
 39/// </example>
 40[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
 41public sealed class DeferToContainerAttribute : Attribute
 42{
 43    /// <summary>
 44    /// Initializes a new instance of <see cref="DeferToContainerAttribute"/>
 45    /// with the specified constructor parameter types.
 46    /// </summary>
 47    /// <param name="constructorParameterTypes">
 48    /// The types of the constructor parameters in order. Each type will be
 49    /// resolved from the service provider when creating an instance.
 50    /// </param>
 451    public DeferToContainerAttribute(params Type[] constructorParameterTypes)
 52    {
 453        ConstructorParameterTypes = constructorParameterTypes ?? Array.Empty<Type>();
 454    }
 55
 56    /// <summary>
 57    /// Gets the constructor parameter types in order.
 58    /// </summary>
 59    /// <remarks>
 60    /// These types are used by the source generator to create a factory delegate
 61    /// that resolves each parameter from the service provider and passes them
 62    /// to the constructor.
 63    /// </remarks>
 1064    public Type[] ConstructorParameterTypes { get; }
 65}