Skip to content

Conversation

@fredrikhr
Copy link
Contributor

@fredrikhr fredrikhr commented Jul 23, 2020

Implements new API proposed in #36209. The proposal was reviewed and approved by @terrajobst on 2020-07-21T18:33:00Z, ref.: #36209 (comment).

Details

The method calls the Configure<TDep> method on the passed optionsBuilder instance, specifying IConfiguration as an injected dependency.

The configureOptions argument for the call to the Configure method is given as a lambda that accepts the IConfiguration root instance from DI. The lambda is now responsible for extracting the appropriate section from that root. This is done by

  1. If configSectionPath is a non-empty string, IConfiguration.GetSection is called with configSectionPath as the argument.
  2. If configSectionPath is null or an empty string, the Name property of the optionsBuilder parameter is used.
  3. As a final fallback the configuration root is used, if both strings above are null or empty.

When the appropriate configuration section has been obtained, the Bind extension method is called, perfoming the binding agrainst the TOptions instance. The configureBinder parameter is specified as nullable, and is passed directly to the Bind extension method. According to the current implementation of ConfigurationBinder.Bind, null is an acceptable value.

Returns

The method returns the value of the optionsBuilder this-parameter to allow for fluid chained method calls.

Exceptions

  • If optionsBuilder is null the method immediately throws a new ArgumentNullException.

fixes #36209, duplicate of dotnet/extensions#3020 and #34191

@fredrikhr
Copy link
Contributor Author

  1. As a final fallback the configuration root is used, if both strings above are null or empty.

@maryamariyan, @davidfowl, maybe we should discuss this behaviour? Should I maybe throw InvalidOperationException or ArgumentException instead?

@fredrikhr
Copy link
Contributor Author

I also wanted to add test-cases for this new API, but I realized after some searching that the Microsoft.Extensions.Options.ConfigurationExtensions library simply does not have its pwn test-project. Should I just create a new project (modelled after one of the test-projects from other libraries), or is there something else I should consider?

@ghost
Copy link

ghost commented Jul 23, 2020

Tagging subscribers to this area: @maryamariyan
See info in area-owners.md if you want to be subscribed.

@maryamariyan
Copy link
Contributor

maryamariyan commented Jul 28, 2020

@fredrikhr are you adding tests for coverage of this API as well?

Perhaps you could add them in https://github.com/dotnet/runtime/blob/master/src/libraries/Microsoft.Extensions.Options/tests/OptionsBuilderTest.cs

Ideally though, if you could add a new test project for Microsoft.Extensions.Options.ConfigurationExtensions and place your tests there it would be better. Because if the test class was under
src/libraries/Microsoft.Extensions.Options.ConfigurationExtensions/tests
folder then you could then check code coverage with: (ignore -c Release if not building in Release) using:

> dotnet build src\libraries\Microsoft.Extensions.Options.ConfigurationExtensions\tests\ /t:test /p:coverage=true -c Release

but for now since all Options tests are under src/libraries/Microsoft.Extensions.Options/tests folder you can add them there instead if you like.

@fredrikhr fredrikhr force-pushed the options-config-binder branch from 3315cf1 to b9628cc Compare July 28, 2020 09:01
@fredrikhr
Copy link
Contributor Author

@maryamariyan I'll start working on a new unit testing projects and add tests.

@fredrikhr
Copy link
Contributor Author

@maryamariyan okay I have added a new Unit test project for MS.Ext.Options.ConfigExt can you look at if what I did is okay? I took inspiration from how the unit test project for MS.Ext.Options work.

For the new API I also enabled nullable annotations using #nullable enable/restore (to avoid touching the existing APIs) in
14598c1. Not that it matters incredibly much in this case, but if I understand correctly this is the way to move forward with new APIs, so why not? We could probably enable for the whole project, but I figured that would be out of scope for this PR.

@fredrikhr fredrikhr force-pushed the options-config-binder branch from 92871b7 to 6f07962 Compare July 29, 2020 12:58
@maryamariyan
Copy link
Contributor

maryamariyan commented Jul 29, 2020

For the new API I also enabled nullable annotations using #nullable enable/restore (to avoid touching the existing APIs) in
14598c1. Not that it matters incredibly much in this case, but if I understand correctly this is the way to move forward with new APIs, so why not? We could probably enable for the whole project, but I figured that would be out of scope for this PR.

I think it's better to add the API without nullable annotations. They would get covered once the entire assembly is annotated. It could make it harder to review later otherwise.

@fredrikhr
Copy link
Contributor Author

@maryamariyan okay your call :)

Copy link
Contributor

@maryamariyan maryamariyan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks @fredrikhr.

EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUtilities", "..\Common\tests\TestUtilities\TestUtilities.csproj", "{3EFE3DA9-F864-40A3-8892-899D731CC027}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "dependencies", "dependencies", "{78CAAC57-641E-4EBC-AD1F-0AA1034C94A2}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: just making a quick comparison style-wise with other sln files in the repo, and we don't seem to have "dependencies" like what you are adding here. e.g. Primitives.

Thanks for adding the test project!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I removed the dependencies solution folder and moved all together into test. ddb583b

@maryamariyan
Copy link
Contributor

@davidfowl @HaoK seems like your feedback are addressed as well.

@maryamariyan maryamariyan merged commit 5870d4f into dotnet:master Aug 5, 2020
@maryamariyan
Copy link
Contributor

Thanks @fredrikhr

@fredrikhr
Copy link
Contributor Author

@maryamariyan merged into master. Does that mean this will actually be part of .NET 5?

@maryamariyan
Copy link
Contributor

@maryamariyan merged into master. Does that mean this will actually be part of .NET 5?

sure thing :)

Jacksondr5 pushed a commit to Jacksondr5/runtime that referenced this pull request Aug 10, 2020
@karelz karelz added this to the 5.0.0 milestone Aug 18, 2020
@davidfowl
Copy link
Member

I think we missed wiring up change notifications as part of this change 😢

@ghost ghost locked as resolved and limited conversation to collaborators Dec 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API Proposal: Add BindConfiguration extension method for OptionsBuilder

5 participants