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
<PackageReference Include="RhoMicro.CodeAnalysis.OptionsGenerator" Version="24.1.9"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="RhoMicro.CodeAnalysis.OptionsGenerator" Version="24.1.9" />
<PackageReference Include="RhoMicro.CodeAnalysis.OptionsGenerator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add RhoMicro.CodeAnalysis.OptionsGenerator --version 24.1.9
#r "nuget: RhoMicro.CodeAnalysis.OptionsGenerator, 24.1.9"
#:package RhoMicro.CodeAnalysis.OptionsGenerator@24.1.9
#addin nuget:?package=RhoMicro.CodeAnalysis.OptionsGenerator&version=24.1.9
#tool nuget:?package=RhoMicro.CodeAnalysis.OptionsGenerator&version=24.1.9
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: apartialdeclaration of the target interface providing a default instanceDefaultFoo: aninternalimplementation ofIFoousingIOptions<MutableFoo>to implement its propertiesSnapshotFoo: aninternalimplementation ofIFoousingIOptionsSnapshot<MutableFoo>to implement its propertiesMonitorFoo: aninternalimplementation ofIFoousingIOptionsMonitor<MutableFoo>to implement its propertiesMutableFoo: aninternalmutable implementation ofIFoofor use in the options patternFoo: apublicimmutable record implementation ofIFoofor public consumptionFooRegistrationStrategy: aninternalhelper type for registering implementations ofIFooto service collectionsFooConfiguration: apublichelper type for configuring registration ofIFooServiceCollectionExtensions: apublichelper type for registering implementations ofIFooto 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
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.13.0)
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 |