Page cover image

Extending - Loaders

It's very easy to add custom providers.

It's very easy to add custom providers. Remember that .NET creates a ConfigurationSource that implements and returns a ConfigurationProvider.

You can see this pattern here: Implement a custom configuration provider.

Custom Implementation

To implement a custom source and provider only requires a single line of code.

  1. Call the ConfigureConfiguration method.

  2. This callback sends you two parameters:

    1. IConfigurationBuilder This builder allows you to add a custom source.

    2. EnivronmentString the string variable that is the current environment.

  3. Simply add a new instance of the source. (See line 5)

PerigeeApplication.ApplicationNoInit("DemoApp", (c) => {
    
    //Add ConfigurationSource to the builder
    c.ConfigureConfiguration((builder, EnvStr) => {
        builder.Add(new SQLConfigurationSource("connectionString", "select [name], [value] from dbo.configs", "sql"));
    });

    //Reload all providers
    c.ReloadProviders();

    //Reload the specific provider
    c.ReloadProvidersOfType(typeof(SQLConfigurationProvider));
    
    string MyValueFromSQL = c.GetValue<string>("sql:MyValue");
    
});

You can see how easy it is in the future to reload the providers. You can reload all of them or specific types by supplying that type information.

Using the built in SQL Provider

If you're trying to load values from a SQL database, then Perigee ships with a loader ready to go in only a single line of code. Simply call the configuration method and supply the connection, query, and prefix.

PerigeeApplication.ApplicationNoInit("DemoApp", (c) => {

    //Configure the SQL Property loader using SqlConnection
    c.ConfigureSqlPropertyLoader(
        () => new SqlConnection(c.GetConnectionString("main")),
        "SELECT [Key],[Value] FROM [dbo].[PropertyStore]");

    string MyValueFromSQL = c.GetValue<string>("sql:MyValue");
    
});

Last updated