| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.Generators; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Generates a factory for this type, allowing runtime parameters to be |
| | | 7 | | /// specified while auto-injecting the rest from the service provider. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// <para> |
| | | 11 | | /// This is a source-generation only feature. It requires the NexusLabs.Needlr.Generators |
| | | 12 | | /// package and has no effect when using reflection-based registration. |
| | | 13 | | /// </para> |
| | | 14 | | /// <para> |
| | | 15 | | /// When applied to a class with mixed injectable and non-injectable constructor parameters, |
| | | 16 | | /// the generator will create: |
| | | 17 | | /// <list type="bullet"> |
| | | 18 | | /// <item><description>A <c>Func<TRuntime..., TService></c> that takes only the non-injectable parameters</descrip |
| | | 19 | | /// <item><description>An <c>I{TypeName}Factory</c> interface with a <c>Create()</c> method</description></item> |
| | | 20 | | /// </list> |
| | | 21 | | /// </para> |
| | | 22 | | /// <para> |
| | | 23 | | /// The type itself is NOT registered in the container - only the factory is. |
| | | 24 | | /// </para> |
| | | 25 | | /// </remarks> |
| | | 26 | | /// <example> |
| | | 27 | | /// <code> |
| | | 28 | | /// using NexusLabs.Needlr.Generators; |
| | | 29 | | /// |
| | | 30 | | /// [GenerateFactory] |
| | | 31 | | /// public class MyService : IMyService |
| | | 32 | | /// { |
| | | 33 | | /// public MyService(IDependency dep, string connectionString) { } |
| | | 34 | | /// } |
| | | 35 | | /// |
| | | 36 | | /// // Generated: IMyServiceFactory with Create(string connectionString) returning MyService |
| | | 37 | | /// // Generated: Func<string, MyService> |
| | | 38 | | /// // Consumer can inject either and create instances with runtime params |
| | | 39 | | /// </code> |
| | | 40 | | /// </example> |
| | | 41 | | [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)] |
| | | 42 | | public sealed class GenerateFactoryAttribute : Attribute |
| | | 43 | | { |
| | | 44 | | /// <summary> |
| | | 45 | | /// Controls what factory artifacts are generated. |
| | | 46 | | /// Default: <see cref="FactoryGenerationMode.All"/> |
| | | 47 | | /// </summary> |
| | | 48 | | public FactoryGenerationMode Mode { get; set; } = FactoryGenerationMode.All; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Generates a factory for this type that returns the specified interface type, |
| | | 53 | | /// allowing runtime parameters to be specified while auto-injecting the rest from the service provider. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <typeparam name="TInterface"> |
| | | 56 | | /// The interface type that the factory's <c>Create()</c> method will return. |
| | | 57 | | /// Must be an interface implemented by the decorated class. |
| | | 58 | | /// </typeparam> |
| | | 59 | | /// <remarks> |
| | | 60 | | /// <para> |
| | | 61 | | /// This is a source-generation only feature. It requires the NexusLabs.Needlr.Generators |
| | | 62 | | /// package and has no effect when using reflection-based registration. |
| | | 63 | | /// </para> |
| | | 64 | | /// <para> |
| | | 65 | | /// Use this generic variant when you need the factory to return an interface type for: |
| | | 66 | | /// <list type="bullet"> |
| | | 67 | | /// <item><description>Mocking the factory's return value in tests</description></item> |
| | | 68 | | /// <item><description>Abstracting the concrete implementation from consumers</description></item> |
| | | 69 | | /// </list> |
| | | 70 | | /// </para> |
| | | 71 | | /// </remarks> |
| | | 72 | | /// <example> |
| | | 73 | | /// <code> |
| | | 74 | | /// using NexusLabs.Needlr.Generators; |
| | | 75 | | /// |
| | | 76 | | /// [GenerateFactory<IMyService>] |
| | | 77 | | /// public class MyService : IMyService |
| | | 78 | | /// { |
| | | 79 | | /// public MyService(IDependency dep, string connectionString) { } |
| | | 80 | | /// } |
| | | 81 | | /// |
| | | 82 | | /// // Generated: IMyServiceFactory with Create(string connectionString) returning IMyService |
| | | 83 | | /// // Generated: Func<string, IMyService> |
| | | 84 | | /// </code> |
| | | 85 | | /// </example> |
| | | 86 | | [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)] |
| | | 87 | | public sealed class GenerateFactoryAttribute<TInterface> : Attribute |
| | | 88 | | where TInterface : class |
| | | 89 | | { |
| | | 90 | | /// <summary> |
| | | 91 | | /// Controls what factory artifacts are generated. |
| | | 92 | | /// Default: <see cref="FactoryGenerationMode.All"/> |
| | | 93 | | /// </summary> |
| | 410 | 94 | | public FactoryGenerationMode Mode { get; set; } = FactoryGenerationMode.All; |
| | | 95 | | } |