Skip to content

Consider supporting configuration via the options pattern #5067

@roji

Description

@roji

The universal way to configure database connections in .NET is the connection string; while this works, it's a pretty outdated model that doesn't correspond well to modern application configuration approaches:

  • Connection strings can only represent a simple bundle of key/value pairs - anything more complex requires complicated, manual string encoding. A good example is encoding multiple hosts - possibly with the ports! - in the connection string, requiring commas etc.
  • Modern .NET applications frequently store hierarchical configuration in JSON, using IConfiguration and the options pattern to access it. Connections strings are embedded as strings within this, with their own internal structure. Ideally, the information in the connection string would simply be laid out in the configuration just like any other hierarchical data.
  • With hierarchy, related settings could be grouped nicely within the config hierarchy, e.g. all connection pooling settings would be organized within their own sub-object.
  • As the possible downside, it may be useful for users to have a simple, encapsulated connection string representation of database config, which can be easily copied and pasted around, rather than e.g. a complex, hierarchical JSON fragment.

An interesting thing that the options pattern could enable is reloading config without restarting the application. Some thoughts:

  • For a very restricted set of options, we may be able to just inject the updated setting into an existing, operational data source. A simple example is the password (already possible programmatically via NpgsqlDataSource.Password), another could be the list of hosts in a multi-host data source (see Dynamically updated MultiHostDataSource host list #5064).
  • We could go even further and allow any option to be changed at runtime; this would entail creating a new data source behind the scene with the updated configuration, and disposing the old one. We could implement this by providing the user with a data source proxy (when config reloading is enabled), and do the swapping under the hood.
  • Strictly-speaking, the online reloading feature could be orthogonal to moving away from connection strings. In other words, the user could modify a connection string embedded in their config, triggering the same swapping mechanism.

Thanks to @NinoFloris for originally raising this in various offline conversations.

/cc @ajcvickers @davidfowl @DamianEdwards

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions