AutoRegisterInject icon indicating copy to clipboard operation
AutoRegisterInject copied to clipboard

Support for Option Pattern

Open timia2109 opened this issue 1 year ago • 2 comments

Hi everyone,

it would be cool if you could add support for the Options Pattern.

If there is at least one class that uses the attribute, the generated method should be: serviceCollection.AutoRegister(IConfiguration configuration);

An attribute might look like this:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class OptionAttribute : Attribute {
    public string? Section {get;}

    public OptionAttribute(string? section = null) {
        Section = section;
    }
}

Possible Usages:

[Option]
public record RootOption {}

[Option("SectionOption")]
public record SectionOption {}

[Option("SectionOption:InnerSection:MoreInnerSection")]
public record InnerSectionOption {}

Thank you

timia2109 avatar Jun 09 '24 13:06 timia2109

There are a number of considerations over both the surface area and extent to which ARI would be able to register options.

Mutability is quite a concern for a lot of people setting these types of objects up.

ARI would need to be able to new up one of these types, which isn't strictly what everyone wants. Since ARI is opinionated this may be ok.

There may be requirement to validate the options and as such provide more fine options within the attribute.

patrickklaeren avatar Jun 11 '24 06:06 patrickklaeren

There are a number of considerations over both the surface area and extent to which ARI would be able to register options.

Mutability is quite a concern for a lot of people setting these types of objects up.

ARI would need to be able to new up one of these types, which isn't strictly what everyone wants. Since ARI is opinionated this may be ok.

There may be requirement to validate the options and as such provide more fine options within the attribute.

It'd be great if you could make it to support the fundamental scenarios around options configuration, at least initially. We could implement a source generator that produces code similar to:

_ = services.AddOptions<TOptions>()
    .BindConfiguration(SectionName) // Support multiple binding approaches
    .ValidateDataAnnotations() // Optional via ARI  
    .ValidateOnStart(); // Optional via ARI

This keeps things simple while covering the most common use cases.

tcortega avatar Nov 01 '24 18:11 tcortega