

The benefits & implementation of the ‘Options Pattern’ in ASP.NET Core
source link: https://www.dave-beaumont.co.uk/2019/11/18/the-benefits-and-implementation-of-the-options-pattern-in-asp-net-core/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The benefits & implementation of the ‘Options Pattern’ in ASP.NET Core
As developers we know configuration is extremely important and is used in most projects. Injecting in IConfiguration
into dependencies and retrieving it by key like this: _configuration["Section:Key"]
is a common approach.
Hard coding strings is nasty and so is leaking configuration into dependencies that may not require it. The Options pattern fixes this allowing configuration to be bound directly to a class. Configuration can then be isolated to where it is required, adhering to the encapsulation principle and separation of concern. This means classes only have access to the section of configuration they require. You can even go a step further and add validation logic too, but I won’t cover that in this post. You can read about this further here. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-3.0#general-options-configuration
Here is a full example.
So firstly we add the Beer
class to the service container with Configure and bind it to the beer configuration section:
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.Configure<Beer>(Configuration.GetSection("Beer")); }
Beer is injected into our BeerController
as IOptions<Beer>
so we call . .Value
which returns type Beer.
public class BeerController : ControllerBase { private readonly Beer _beer; public BeerController(IOptions<Beer> beer) { _beer = beer?.Value; } [HttpGet] public IActionResult Get() { return Ok(); } }
We can now see the _beer
variable is populated with the correct values pulled from the appsettings.json.

And that’s it, we have injected in the configuration section we require without pointlessly injecting in the whole app configuration, and also it is typed directly to a class, meaning we can use it like this:
var peroniOrigin = _beer.Peroni;
Recommend
-
50
This article explores the Factory Method design pattern and its implementation in Python. Design patterns became a popular topic in late 90s after the so-called Gang of Four (GoF: Gamma, Helm, Johson, and Vlissides) publ...
-
60
README.md Money
-
40
Revised implementation of Strategy Pattern in JavaHow Java 8 killed the Strategy Pattern
-
10
This is the second part of Rust mini-series. Those of you who haven’t seen the first part yet are encouraged totake a look at it. Before we begin, let me point out that the aim of the series is to give a gentle i...
-
4
Anonymous Implementation Classes – A Design Pattern for C# posted by Craig Gidney on October 9, 2012 Some int...
-
8
-
7
Lazy Load Design Pattern in Java: An Introduction and Implementation Watch the video in the article below on lazy load design pa...
-
7
The benefits & implementation of the ‘Options Pattern’ in ASP.NET Core As developers...
-
2
什么是函数选项模式 大家好,我是小白,有点黑的那个白。 最近遇到一个问题,因为业务需求,需要对接三方平台. 而三方平台提供的一些HTTP(S)接口都有统一的密钥生成规则要求. 为此我们封装了一个独立的包 xxx-go-sdk 以便维护和...
-
4
Codeless Test Automation Benefits & Options
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK