RhoMicro.CodeAnalysis.OptionsGenerator 24.1.9

Prefix Reserved
dotnet add package RhoMicro.CodeAnalysis.OptionsGenerator --version 24.1.9
                    
NuGet\Install-Package RhoMicro.CodeAnalysis.OptionsGenerator -Version 24.1.9
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RhoMicro.CodeAnalysis.OptionsGenerator" Version="24.1.9">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RhoMicro.CodeAnalysis.OptionsGenerator" Version="24.1.9" />
                    
Directory.Packages.props
<PackageReference Include="RhoMicro.CodeAnalysis.OptionsGenerator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RhoMicro.CodeAnalysis.OptionsGenerator --version 24.1.9
                    
#r "nuget: RhoMicro.CodeAnalysis.OptionsGenerator, 24.1.9"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RhoMicro.CodeAnalysis.OptionsGenerator@24.1.9
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RhoMicro.CodeAnalysis.OptionsGenerator&version=24.1.9
                    
Install as a Cake Addin
#tool nuget:?package=RhoMicro.CodeAnalysis.OptionsGenerator&version=24.1.9
                    
Install as a Cake Tool

OptionsGenerator

This generator generates an opinionated implementation of the options pattern against an interface defining the options type to use. It allows you to invert control of choosing the desired options lifetime (default, snapshot, monitor) from inside the consumer to the registration site.

The use pattern changes from

class Service(IOptions<Foo> fooOptions);

to

class Service(IFoo foo);

This is an opinionated design that aligns with my use of the pattern.

Installation

<PackageReference Include="RhoMicro.CodeAnalysis.OptionsGenerator" Version="*">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

Quick Start

Define:

using RhoMicro.CodeAnalysis;

[Options]
public interface IFoo
{
    string Setting { get; }
}

Register:

services.AddFoo();

Inject:

class Service(IFoo foo)
{
    public void DoStuff()
    {
        Console.WriteLine(foo.Setting);
    }
}

Use

Generated Code

For a given target interface IFoo, the following type declarations are generated into the namespace containing the target interface:

  • IFoo : a partial declaration of the target interface providing a default instance
  • DefaultFoo : an internal implementation of IFoo using IOptions<MutableFoo> to implement its properties
  • SnapshotFoo : an internal implementation of IFoo using IOptionsSnapshot<MutableFoo> to implement its properties
  • MonitorFoo : an internal implementation of IFoo using IOptionsMonitor<MutableFoo> to implement its properties
  • MutableFoo : an internal mutable implementation of IFoo for use in the options pattern
  • Foo : a public immutable record implementation of IFoo for public consumption
  • FooRegistrationStrategy : an internal helper type for registering implementations of IFoo to service collections
  • FooConfiguration : a public helper type for configuring registration of IFoo
  • ServiceCollectionExtensions : a public helper type for registering implementations of IFoo to service collections

Annotate the Target Interface

Annotate the interface defining your options type with the Options attribute:

using RhoMicro.CodeAnalysis;

[Options]
public interface IFoo;

Exclude Properties

Exclude properties from generated types using the ExcludeFromOptions attribute:

using RhoMicro.CodeAnalysis;

[Options]
public interface IFoo
{
    [ExcludeFromOptions]
    string ExcludedProperty { get; }
}

Register Options to Services

Register options to service collections via the generated extension method:

services.AddFoo(c => c.UseDefaultOptions());
services.AddFoo(c => c.UseSnapshotOptions());
services.AddFoo(c => c.UseMonitorOptions());
services.AddFoo(c => c.UseCustomOptions(sp => Foo.Default with { Setting = "Hello, World!" }));

Configure Options Pattern Implementation

Configure the way that options based implementations are configured by implementing the following partial hook method:

partial class BarRegistrationStrategy
{
    partial class Pattern<T>
    {
        static partial void ConfigureOptionsBuilder(
            OptionsBuilder<MutableBar> builder,
            BarConfiguration configuration)
            => builder.ValidateDataAnnotations().ValidateOnStart();
    }
}

Place Custom Attributes on Properties

Any attribute besides ExcludeFromOptions will be included on all implementations of the interface:

using RhoMicro.CodeAnalysis;
using System.ComponentModel.DataAnnotations;

[Options]
public interface IFoo
{
    [DisallowedValues([null])]
    string StringProperty { get; }
}

The generated types will reflect this annotation:

public sealed partial record Foo
{
    [DisallowedValues([null])]
    string StringProperty { get; init; }
}

Restrictions

  • only interfaces may be targeted by the generator
  • the target interface declaration must be partial
  • the target interface must not be generic
  • only properties will be implemented
  • included properties must be readonly
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
24.1.9 134 6/30/2026
24.1.8 116 6/14/2026
24.1.1 124 4/20/2026
24.1.0 132 1/26/2026
24.0.0 127 1/22/2026
23.2.2 140 1/4/2026
23.2.1 132 1/4/2026
23.1.0 199 12/21/2025
23.0.0 203 12/21/2025
22.0.3 227 8/20/2025
22.0.2 208 8/20/2025
22.0.1 174 7/12/2025
21.1.2 234 6/25/2025
21.1.0 231 6/25/2025
21.0.6 237 6/24/2025
20.1.5 350 5/22/2025
20.1.4 201 5/17/2025
20.1.2 215 5/11/2025