-
Notifications
You must be signed in to change notification settings - Fork 0
Options Generator
Tiago Rios edited this page Nov 4, 2022
·
2 revisions
A common practice while developing .NET applications is to create strongly typed options classes. This allows you to configure object properties' values in your appsettings.json, bind them against your class instance and add it to the dependency injection container. Microsoft's documentation for the Options pattern can be found here.
The source generator includes an OptionsAttribute class, which should be used in your classes that will be used as typed options. The attribute class looks like the following:
using System;
namespace ServiceCollectionGenerators.Attributes
{
[AttributeUsage(AttributeTargets.Class)]
internal class OptionsAttribute : Attribute
{
public string ConfigurationSectionName { get; set; }
public bool ValidateDataAnnotations { get; set; }
public bool ValidateOnStart { get; set; }
}
}The simplest way to use the attribute is:
namespace My.Namespace
{
[Options]
public class DatabaseContextOptions
{
[Required] public string ConnectionString { get; set; }
}
}This will generate the following code:
namespace My.Namespace.Extensions
{
public static partial class ServiceCollectionExtensions
{
internal static IServiceCollection RegisterOptions(this IServiceCollection services) => services.RegisterOptionsForMyNamespace();
public static IServiceCollection RegisterOptionsForMyNamespace(this IServiceCollection services)
{
services.AddOptions<My.Namespace.DatabaseContextOptions>().Bind(configuration.GetSection("DatabaseContextOptions")).ValidateDataAnnotations();
return services;
}
}
}The properties exposed through the attribute allows you to configure how the code is generated.
-
ConfigurationSectionName- According with the generated code above, the value of this property will be used as a parameter of the
configuration.GetSectionmethod. If not specified, this will be the class name where the attribute is being used on.
- According with the generated code above, the value of this property will be used as a parameter of the
-
ValidateDataAnnotations- This extension method tells the code that it should validate the data annotations attributes, if they exist. If not specified, this value will be true and therefore the method
.ValidateDataAnnotations()will be added to the chain.
- This extension method tells the code that it should validate the data annotations attributes, if they exist. If not specified, this value will be true and therefore the method
-
ValidateOnStart- Starting from .NET 6, Microsoft allows you to run
.ValidateDataAnnotations()during the application's startup. By default, this method call is not added to the chain. - As specified in the documentation, this method needs to be used together and after
.ValidateDataAnnotations(). Therefore, in order to protect against the possibility of makingValidateDataAnnotations = falseandValidateOnStart = true, the source generator has a diagnostic implementedOSG001that prevents this situation to happen.
- Starting from .NET 6, Microsoft allows you to run