Skip to content

GenerateFactoryAttribute

NexusLabs.Needlr.Generators

GenerateFactoryAttribute Class

Generates a factory for this type, allowing runtime parameters to be specified while auto-injecting the rest from the service provider.

public sealed class GenerateFactoryAttribute : System.Attribute

Inheritance System.Object 🡒 System.Attribute 🡒 GenerateFactoryAttribute

Example

using NexusLabs.Needlr.Generators;

[GenerateFactory]
public class MyService : IMyService
{
    public MyService(IDependency dep, string connectionString) { }
}

// Generated: IMyServiceFactory with Create(string connectionString) returning MyService
// Generated: Func<string, MyService>
// Consumer can inject either and create instances with runtime params

Remarks

This is a source-generation only feature. It requires the NexusLabs.Needlr.Generators package and has no effect when using reflection-based registration.

When applied to a class with mixed injectable and non-injectable constructor parameters, the generator will create: - A Func<TRuntime..., TService> that takes only the non-injectable parameters - An I{TypeName}Factory interface with a Create() method

The type itself is NOT registered in the container - only the factory is.

Properties

GenerateFactoryAttribute.Mode Property

Controls what factory artifacts are generated. Default: All

public NexusLabs.Needlr.Generators.FactoryGenerationMode Mode { get; set; }

Property Value

FactoryGenerationMode