tiagojsrios.ServiceCollectionGenerators 1.0.0

Suggested Alternatives

ServiceCollectionGenerators 0.1.0

Additional Details

This package is now deprecated, as NuGet's ID has changed. Version 0.1.0 of the new package contains exactly the same source code.

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package tiagojsrios.ServiceCollectionGenerators --version 1.0.0
NuGet\Install-Package tiagojsrios.ServiceCollectionGenerators -Version 1.0.0
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="tiagojsrios.ServiceCollectionGenerators" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add tiagojsrios.ServiceCollectionGenerators --version 1.0.0
#r "nuget: tiagojsrios.ServiceCollectionGenerators, 1.0.0"
#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.
// 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.

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

Created source generator that adds Options classes to the dependency injection container