Skip to content

GenerateFactoryAttribute TInterface

NexusLabs.Needlr.Generators

GenerateFactoryAttribute<TInterface> Class

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

public sealed class GenerateFactoryAttribute<TInterface> : System.Attribute
    where TInterface : class

Type parameters

TInterface

The interface type that the factory's Create() method will return. Must be an interface implemented by the decorated class.

Inheritance System.Object 🡒 System.Attribute 🡒 GenerateFactoryAttribute\<TInterface>

Example

using NexusLabs.Needlr.Generators;

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

// Generated: IMyServiceFactory with Create(string connectionString) returning IMyService
// Generated: Func<string, IMyService>

Remarks

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

Use this generic variant when you need the factory to return an interface type for: - Mocking the factory's return value in tests - Abstracting the concrete implementation from consumers

Properties

GenerateFactoryAttribute<TInterface>.Mode Property

Controls what factory artifacts are generated. Default: All

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

Property Value

FactoryGenerationMode