Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

BCL API review: ConfigurationBinder feedback #236

@divega

Description

@divega

The ConfigurationBinder static class is considered DNX specific and embodies the ability to apply configuration values from an IConfiguration to the matching properties of an instance of a simple class. It is used in the implementation of Options.

Currently there are two public Bind methods in it:

public static TModel Bind<TModel>(IConfiguration configuration) where TModel : new()

public static void Bind(object model, IConfiguration configuration)

The methods have different purposes, e.g. the generic version will create an object of type TModel while the no-generic one receives the object. The implementation of the latter no-ops if the objects passed is null.

We want to rationalize this API, including revisiting the names of the class and methods and also the signatures and the behavior of the methods. After gathering feedback from the BCL team and then having some discussions on our own we have come up with this API:

public static TModel Bind<TModel>(this IConfiguration configuration) where TModel : class, new()
public static TModel Bind<TModel>(this IConfiguration configuration, TModel model)  where TModel : class

// Usage:

var orgSettings = configuration.GetSection("Org").Bind<OrgSettings>();
var tenantSettings = configuration.GetSection("Tenant1").Bind(new AppSettings());

Notice that:

  1. Bind overloads are extension methods on configuration
  2. They return the instance of TModel, regarless of whether it got passed or created by the call.
  3. They don't support structs.
  4. The overloads that take a TModel argument should throw if a null model is passed.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions