< Summary

Information
Class: NexusLabs.Needlr.Generators.ProviderAttribute
Assembly: NexusLabs.Needlr.Generators.Attributes
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Generators.Attributes/ProviderAttribute.cs
Line coverage
100%
Covered lines: 9
Uncovered lines: 0
Coverable lines: 9
Total lines: 127
Line coverage: 100%
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_Required()100%11100%
get_Optional()100%11100%
get_Collections()100%11100%
get_Factories()100%11100%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Generators.Attributes/ProviderAttribute.cs

#LineLine coverage
 1using System;
 2
 3namespace NexusLabs.Needlr.Generators;
 4
 5/// <summary>
 6/// Marks an interface or partial class as a Provider - a strongly-typed service locator.
 7/// </summary>
 8/// <remarks>
 9/// <para>
 10/// A Provider is a compile-time generated class that provides strongly-typed access to
 11/// services registered in the dependency injection container. Unlike using
 12/// <c>IServiceProvider.GetService&lt;T&gt;()</c> directly, Providers offer:
 13/// </para>
 14/// <list type="bullet">
 15/// <item><description>Compile-time verification that required services are registered</description></item>
 16/// <item><description>IntelliSense and IDE support for available services</description></item>
 17/// <item><description>Easy mocking in unit tests</description></item>
 18/// </list>
 19/// <para>
 20/// <b>Providers are always registered as Singletons.</b> All service properties are resolved
 21/// via constructor injection at Provider construction time (fail-fast). For creating new
 22/// instances on demand, use the <see cref="Factories"/> parameter to generate factory properties.
 23/// </para>
 24/// <para>
 25/// <b>Usage modes:</b>
 26/// </para>
 27/// <list type="number">
 28/// <item>
 29/// <description>
 30/// <b>Interface definition:</b> Apply to an interface with get-only properties.
 31/// The generator creates an implementing class.
 32/// <code>
 33/// [Provider]
 34/// public interface IOrderServicesProvider
 35/// {
 36///     IOrderRepository Repository { get; }
 37///     IOrderValidator Validator { get; }
 38/// }
 39/// // Generates: OrderServicesProvider class
 40/// </code>
 41/// </description>
 42/// </item>
 43/// <item>
 44/// <description>
 45/// <b>Shorthand class:</b> Apply to a partial class with type parameters.
 46/// The generator creates both an interface and the implementation.
 47/// <code>
 48/// [Provider(typeof(IOrderRepository), typeof(IOrderValidator))]
 49/// public partial class OrderDependenciesProvider { }
 50/// // Generates: IOrderDependenciesProvider interface + implementation
 51/// </code>
 52/// </description>
 53/// </item>
 54/// </list>
 55/// </remarks>
 56[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
 57public sealed class ProviderAttribute : Attribute
 58{
 59    /// <summary>
 60    /// Creates a Provider attribute for an interface definition.
 61    /// Define service properties directly on the interface.
 62    /// </summary>
 184563    public ProviderAttribute()
 64    {
 184565    }
 66
 67    /// <summary>
 68    /// Creates a Provider with the specified required service types.
 69    /// Use on a partial class to auto-generate interface and implementation.
 70    /// </summary>
 71    /// <param name="requiredServices">
 72    /// Service types that must be registered in the container.
 73    /// Property names are derived from type names (e.g., IOrderRepository → OrderRepository).
 74    /// </param>
 123075    public ProviderAttribute(params Type[] requiredServices)
 76    {
 123077        Required = requiredServices;
 123078    }
 79
 80    /// <summary>
 81    /// Service types that must be registered. Resolution failure throws at startup.
 82    /// </summary>
 83    /// <remarks>
 84    /// Properties for these services use <c>GetRequiredService&lt;T&gt;()</c>.
 85    /// If any service is not registered, the application fails to start.
 86    /// </remarks>
 123087    public Type[]? Required { get; set; }
 88
 89    /// <summary>
 90    /// Service types that may not be registered. Properties are nullable.
 91    /// </summary>
 92    /// <remarks>
 93    /// Properties for these services use <c>GetService&lt;T&gt;()</c> and return null
 94    /// if the service is not registered.
 95    /// </remarks>
 61596    public Type[]? Optional { get; set; }
 97
 98    /// <summary>
 99    /// Service types to resolve as <c>IEnumerable&lt;T&gt;</c>.
 100    /// </summary>
 101    /// <remarks>
 102    /// Properties for these services use <c>GetServices&lt;T&gt;()</c> and return
 103    /// all registered implementations.
 104    /// </remarks>
 615105    public Type[]? Collections { get; set; }
 106
 107    /// <summary>
 108    /// Types to generate factories for, enabling creation of new instances.
 109    /// </summary>
 110    /// <remarks>
 111    /// <para>
 112    /// For each type specified:
 113    /// </para>
 114    /// <list type="bullet">
 115    /// <item><description>
 116    /// If the type has <c>[GenerateFactory]</c>, the existing factory is used.
 117    /// </description></item>
 118    /// <item><description>
 119    /// Otherwise, a factory (<c>IXxxFactory</c>) is generated automatically.
 120    /// </description></item>
 121    /// </list>
 122    /// <para>
 123    /// This allows the Provider to create new instances while remaining a Singleton.
 124    /// </para>
 125    /// </remarks>
 615126    public Type[]? Factories { get; set; }
 127}