Configuration

Configuration

🌍 Language: English | Español

Layered configuration system for Delphi inspired by .NET’s Microsoft.Extensions.Configuration. Read settings from JSON files, INI files, environment variables, in-memory dictionaries or chained sources — all unified behind a single IConfiguration interface.

Delphi 12+ License


Why use this?

  • 📄 Multiple sources — JSON, INI, environment variables, in-memory, or custom
  • 🔗 Layered & overridable — sources are chained; later sources override earlier ones
  • 🗂️ Hierarchical keys — use : as separator (Database:ConnectionString)
  • 🔄 Reload supportIConfigurationRoot.Reload re-reads all providers
  • 🎯 Binder — map a configuration section directly to a Delphi class

Quick Start

uses
  Daf.Configuration.Builder,
  Daf.Configuration.Json,
  Daf.Configuration.Env,
  Daf.Extensions.Configuration;

var Config := TConfigurationBuilder.Create
  .AddJsonFile('appsettings.json')
  .AddEnvironmentVariables
  .Build;

// Read a value
var ConnStr := Config['Database:ConnectionString'];

// Read a section
var DbSection := Config.GetSection('Database');
var Host := DbSection['Host'];

Supported Providers

Provider Unit Description
JSON file Daf.Configuration.Json appsettings.json, optional/required
INI file Daf.Configuration.Ini Classic [Section]\nKey=Value format
Environment variables Daf.Configuration.Env OS env vars, optionally filtered by prefix
In-memory Daf.Configuration.Memory Dictionary of string → string pairs
Chained Daf.Configuration.Chained Wrap an existing IConfiguration as a source

Binding to Objects

Use TConfigurationBinder to populate a Delphi object from a configuration section:

uses Daf.Configuration.Binder;

type
  TDatabaseOptions = class
  public
    Host: string;
    Port: Integer;
    Name: string;
  end;

var Opts := TDatabaseOptions.Create;
TConfigurationBinder.Bind(Config.GetSection('Database'), Opts);
// Opts.Host, Opts.Port, Opts.Name are now populated

Integration with Hosting

When using Hosting, configure sources in ConfigureAppConfiguration:

THostBuilder.Create
  .ConfigureAppConfiguration(procedure(Ctx: IHostBuilderContext;
                                       Builder: IConfigurationBuilder)
  begin
    Builder
      .AddJsonFile('appsettings.json')
      .AddJsonFile('appsettings.' + string(Ctx.Environment.EnvironmentName) + '.json', True)
      .AddEnvironmentVariables;
  end)
  .ConfigureServices(procedure(Ctx: IHostBuilderContext; Services: IServiceCollection)
  begin
    // Ctx.Configuration is fully built here
    Services.AddSingleton<IMyOptions>(TMyOptions.Create(Ctx.Configuration));
  end)
  .Build.Run;

Documentation

  • 📖 Usage Guide — providers in depth, hierarchical keys, binder, custom sources

Abstractions

Configuration.Abstractions

🌍 Language: English | Español

Core contracts for the DAF configuration system. This module defines interfaces and types only — no implementation. Reference it in libraries that need to read configuration without being bound to a specific provider.

Delphi 12+ License


What’s in this module

All types live in Daf.Extensions.Configuration.

Core interfaces

Interface Role
IConfiguration Read/write key-value store with section navigation
IConfigurationSection A named subtree of IConfiguration — adds Key, Path, Value, HasChildren
IConfigurationRoot Root of the tree — adds Reload and Providers
IConfigurationBuilder Fluent builder: add sources, then call Build
IConfigurationProvider Single source implementation: TryGet, Set, Load, GetChildKeys
IConfigurationSource Factory for a provider: Build(Builder): IConfigurationProvider

Key access

Config['Database:Host']               // direct key
Config.GetSection('Database')['Host'] // via section

Key separator is :. Sections expose GetChildren to iterate sub-keys.

TConfigurationPath

Utility class for path manipulation:

TConfigurationPath.Combine('Database', 'Host')  // 'Database:Host'
TConfigurationPath.GetSectionKey('Database:Host') // 'Host'
TConfigurationPath.GetParentPath('Database:Host') // 'Database'

TConfigurationSourceOption

type TConfigurationSourceOption = (csoOptional, csoReloadOnChange);

Dependencies

No dependencies on other DAF modules. Delphi RTL only.

The implementation is in Configuration.