JasperFx 0.1.0


Next

Jasper Service Bus

Previous

Getting Started

Configuration Edit on GitHub


Quick Start

Probably the most common scenario is to have a single configuration file mapped to a single object:

  1. Add a class that ends with Settings to your project, e.g. MySettings.cs.
  2. Add a json file that has properties that match your Settings class.
  3. Use the Build method to tell Jasper about your configuration file.
  4. Include your Settings class in the constructor of a class and Jasper will automatically inject the settings object

public class MyApp : JasperRegistry
{
    public MyApp()
    {
        Settings.Build(_ =>
        {
            _.AddJsonFile("mysettings.json");
        });
    }
}

public class SettingsTest
{
    private readonly MySettings _settings;

    public SettingsTest(MySettings settings)
    {
        _settings = settings;
    }
}

Add Configuration Sources

Jasper uses the .NET Core ConfigurationBuilder to get config data and bind it to objects.


Settings.Build(_ =>
{
    _.SetBasePath("path");
    _.AddJsonFile("myconfig.json");
    _.AddJsonFile("myotherconfig.json.config");
    _.AddEnvironmentVariables();
});

If you need to bind a settings class that does not follow the convention of ending with Settings then use the Configure method to tell Jasper which class you want to bind.


Settings.Configure<Colors>();

If a settings class needs additional information to bind correctly, such as being in a nested sub-section, use the Configure method.


Settings.Configure<MySettings>(_ => _.GetSection("subsection"));

Modify Settings

It may be necessary to modify a settings object after it has been loaded from configuration. Settings can be altered:


Settings.Alter<MySettings>(_ =>
{
    _.SomeSetting = 5;
});

or completely replaced:


Settings.Replace(new MySettings
{
    SomeSetting = 3,
    OtherSetting = "blue"
});

Modify Application

The JasperRegistry (or the application that inherits from JasperRegistry) can be modified using loaded settings:


public class MyApplication : JasperRegistry
{
    public bool MyBoolean { get; set; }

    public MyApplication()
    {
        Settings.With<MySettings>(_ =>
        {
            if (_.SomeSetting == 1)
            {
                MyBoolean = true;
            }
        });
    }
}