question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refactor configuration to take advantage of Options Pattern

See original GitHub issue

Current .NET Core configuration utilizes a non-conventional pattern to load configuration values.

https://github.com/umbraco/Umbraco-CMS/blob/43efee9647ed8f2eaab3ec2b71d3dd3eb46b9017/src/Umbraco.Configuration/AspNetCoreConfigsFactory.cs

ASP.NET Core already has an established pattern to load options. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-3.1

There are several advantages to using this pattern.

  1. It’s common convention. No surprises for future contributors.
  2. It allows options monitoring and dynamic reloading when required.
  3. It negates the requirement for each superfluous interface definition and implementation.

Binding a section becomes trivial.

services.Configure<MyOptions>(Configuration.GetSection(nameof(MyOptions)));

Consuming it equally so.

public class MyOptionsConsumer(IOptions<MyOptions> options)
{
    Guard.NotNull(options, nameof(options));

    this.options = options.Value;    
}

Unit testing again is trivial.

var myOptions = new MyOptions();
var myOptionsConsumer = new MyOptionsConsumer(Options.Create(myOptions));

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
AndyButlandcommented, Oct 14, 2020

Can we/should we close this now @bergmania? Seems that any further uses of IOptions will come in as and when new features are migrated, rather than being a separate task to add.

2reactions
bergmaniacommented, Aug 20, 2020

We decide to allow the MS Config interfaces in Umbraco.Core. @AndyButland already started a draft PR. Good start, and we can continue on work.

I think we should make a netcore/config branch as the base branch for all these smaller config PRs, so we can merge these into netcore/netcore as one big PR, but review it as several smaller PRs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Options pattern in ASP.NET Core
The options pattern uses classes to provide strongly typed access to groups of related settings. When configuration settings are isolated by ...
Read more >
Strongly Typed Configurations with Options Pattern
The options pattern uses classes to provide strongly typed access to groups of related settings. The Options pattern adheres to the following ...
Read more >
ASP.NET Core Configuration – Options Pattern
The options pattern gives us similar possibilities, but it offers a more structured approach and more features like validation, live reloading, ...
Read more >
ASP.NET Core - Accessing Configurations Using Options ...
The main advantage of options pattern is that we create classes and each class which will have properties for configuring the specific parts ......
Read more >
The Options pattern | An Atypical ASP.NET Core 6 Design ...
The Options pattern's goal is to use settings at runtime, allowing changes to the application to happen without changing the code. The settings...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found