| | | 1 | | namespace 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<MyService>))] |
| | | 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<MyService> logger) { } |
| | | 33 | | /// |
| | | 34 | | /// // Needlr generates factory based on your [DeferToContainer] declaration: |
| | | 35 | | /// // sp => new MyService( |
| | | 36 | | /// // sp.GetRequiredService<IRepository>(), |
| | | 37 | | /// // sp.GetRequiredService<ILogger<MyService>>()) |
| | | 38 | | /// </code> |
| | | 39 | | /// </example> |
| | | 40 | | [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)] |
| | | 41 | | public 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> |
| | 4 | 51 | | public DeferToContainerAttribute(params Type[] constructorParameterTypes) |
| | | 52 | | { |
| | 4 | 53 | | ConstructorParameterTypes = constructorParameterTypes ?? Array.Empty<Type>(); |
| | 4 | 54 | | } |
| | | 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> |
| | 10 | 64 | | public Type[] ConstructorParameterTypes { get; } |
| | | 65 | | } |