Vostok.Configuration
HomeQuickstartConcepts and basicsBasic scenarios
  • Home
  • Quickstart
  • Concepts and basics
    • Settings nodes
      • Value nodes
      • Array nodes
      • Object nodes
    • Settings nodes merging
    • Settings nodes scoping
    • Binding nodes to models
    • Configuration sources
    • Configuration provider
    • Caching and performance
    • Error handling
  • Modules
    • Abstractions
    • Configuration
    • Sources
    • Sources.CC
    • Sources.Json
    • Sources.Yaml
    • Sources.Xml
    • Sources.Vault
    • Logging
    • Microsoft
  • Sources
    • Constant sources
    • Object source
    • XML sources
    • YAML sources
    • JSON sources
    • Vault source
    • ClusterConfig source
    • Command line source
    • Environment variables source
  • Binders
    • Primitives
    • Collections
    • Classes and structs
    • Constructor injection
  • Basic scenarios
    • Assign sources to types
    • Obtain settings from provider
    • Observe settings via provider
    • Print settings
    • Log settings updates
    • Log errors
    • Combine sources
    • Scope sources
    • Make settings secret
    • Make settings required
  • Advanced scenarios
    • Use name aliases
    • Use dynamic interfaces
    • Use shared provider instance
    • Use value substitutions
    • Nest sources
    • Freeze sources
    • Transform sources
    • Create custom sources
    • Apply custom validators
    • Apply custom binders
    • Apply source data to existing object
    • Print contents of a source
Powered by GitBook
On this page
  • Binders
  • Overview
  • Error handling
  • Related pages

Was this helpful?

Export as PDF
  1. Concepts and basics

Binding nodes to models

PreviousSettings nodes scopingNextConfiguration sources

Last updated 4 years ago

Was this helpful?

Binding is the process of initializing a model (an instance of almost arbitrary type) with data from a obtained from a :

node {A: 1, B: 2} --> new CustomModel { A = 1, B = 2}

The resulting model is queried by the application code with a to access settings.

Binders

Binding is implemented by a set of , each of which knows how to convert a to an object of a specific type.

Binders are composable: if there's a registered binder for Dictionary<T1, T2>, string and int, then it's possible to bind to Dictionary<string, int>. This is heavily used for .

Overview

A typical binding process starts with a and proceeds downward by matching fields and properties with node subtrees by names and invoking appropriate binders:

Node:

{
  Timeout1: "1 seconds",
  Timeout2: "2 seconds"
  Logging:
  {
    Enabled: "true",
    Levels: ["Info", "Warn"]
  }
}
Model:

class AppSettings 
{
    public TimeSpan Timeout1 { get; }
    public TimeSpan Timeout2 { get; }
    public LoggingSettings Logging { get; }
    
    class LoggingSettings
    {
        public bool Enabled { get; }
        public LogLevel[] Levels { get; }
    }
}
Binding process:

Node --> AppSettings via ClassStructBinder
  Node["timeout1"] --> Timeout1 via PrimitiveBinder
  Node["timeout2"] --> Timeout2 via PrimitiveBinder
  Node["logging"] --> Logging via ClassStructBinder
    Node["logging"]["enabled"] --> Enabled via PrimitiveBinder
    Node["logging"]["levels"] --> Levels via ArrayBinder
      Node["logging"]["levels"][0] --> Levels[0] by EnumBinder
      Node["logging"]["levels"][1] --> Levels[1] by EnumBinder

Error handling

In case of failure, a complete list of all errors is presented in resulting exception.

Related pages

See all to learn more about this process.

Binding fails if there's at least one error on any level. Errors may arise from incorrect value formats for , missing values for fields and properties, mismatches of types or missing for requested types.

settings tree
configuration source
configuration provider
binders
settings node
collections
class binder
scoped
binders descriptions
primitives
required
settings node
binders
Settings nodes
Settings nodes scoping
Binders