tiagojsrios.ServiceCollectionGenerators
1.0.0
ServiceCollectionGenerators 0.1.0
Additional DetailsThis package is now deprecated, as NuGet's ID has changed. Version 0.1.0 of the new package contains exactly the same source code.
dotnet add package tiagojsrios.ServiceCollectionGenerators --version 1.0.0
NuGet\Install-Package tiagojsrios.ServiceCollectionGenerators -Version 1.0.0
<PackageReference Include="tiagojsrios.ServiceCollectionGenerators" Version="1.0.0" />
paket add tiagojsrios.ServiceCollectionGenerators --version 1.0.0
#r "nuget: tiagojsrios.ServiceCollectionGenerators, 1.0.0"
// Install tiagojsrios.ServiceCollectionGenerators as a Cake Addin
#addin nuget:?package=tiagojsrios.ServiceCollectionGenerators&version=1.0.0
// Install tiagojsrios.ServiceCollectionGenerators as a Cake Tool
#tool nuget:?package=tiagojsrios.ServiceCollectionGenerators&version=1.0.0
ServiceCollectionGenerators
OptionsAttribute
Another common practice while developing .NET applications is to create binding classes. This allows you to configure object properties' values in appsettings.json
, create and add an object to the dependency injection container as a IOptions<T>
.
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 generated code will make use of the .ValidateDataAnnotations()
method. If needed, you can change this behaviour by set the attribute property ValidateDataAnnotations
to false.
The attribute also allows you to change the Configuration Section name that will be used to bind the Options from the IConfiguration object.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.Extensions.Configuration.Abstractions (>= 2.2.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Options (>= 2.2.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 2.2.0)
- Microsoft.Extensions.Options.DataAnnotations (>= 2.2.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 |
---|
Created source generator that adds Options classes to the dependency injection container