You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Why should we use IOptions<T> when we can inject the configuration object directly. #9663
Hello, by using IOptions<T> we are taking a dependency on microsoft.extensions.options in every library. This page does not go into details about why we can not inject the configuration object T directly into constructor? Even if we inject IOptions<T> we are still grabbing the options.value in the constructor and assigning to a local variable. Can you explain explicit reasons why we should use IOptions<T> ?
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
ID: ed39cb0e-aa85-8bba-ffb4-859b56a60723
Version Independent ID: dc7e3449-650b-d117-5eac-69e5ccb250cb
The purpose of Options is to meet the goals laid out in the opening paragraph of the topic: ISP and separation of concerns. I'm not aware of additional benefits, but let's ask if there's more to the story ...
Options also has validation now which we are not going to do at the configuration level, so that is an additional benefit that comes along with using options
@charinirao I'm working on the PR for this update now. Turns out we did cover validation ... it's in the 2.2 version of the topic. The 2.2 version will become the default version of the topic after 2.2 RTM.
Hi guys! It’s not quite clear to me why validation of options is needed, since it is possible in your Ioc container to register dependency resolution for your IOptions<> - I'm found that this case is work fine:
publicstaticvoidRegisterAppSettings(thisContainerBuilderbuilder){builder.Register(context =>{varappSettingsOptions=context.Resolve<IOptions<AppSettings>>();if(appSettingsOptions?.Value==null)thrownewArgumentNullException(nameof(appSettingsOptions));if(appSettingsOptions.Value.ConnectionStrings==null)thrownewArgumentNullException(nameof(appSettingsOptions),"Section \"ConnectionStrings\" in configuration file is null");if(appSettingsOptions.Value.MailSettings==null)thrownewArgumentNullException(nameof(appSettingsOptions),"Section \"MailSettings\" in configuration file is null");returnappSettingsOptions.Value;}).InstancePerLifetimeScope();}
In my opinion, options validation is the developer responsibility, and if developer does not describe validation rules (in Ioc, for example), then it becomes a problem of developer :)
May be I'm wrong - tell me, please, if this is so.
Activity
guardrex commentedon Nov 21, 2018
The purpose of Options is to meet the goals laid out in the opening paragraph of the topic: ISP and separation of concerns. I'm not aware of additional benefits, but let's ask if there's more to the story ...
@HaoK Thoughts?
HaoK commentedon Nov 21, 2018
Options also has validation now which we are not going to do at the configuration level, so that is an additional benefit that comes along with using options
guardrex commentedon Nov 21, 2018
Thanks ... I'll see about getting that in along with the work for #9662.
suwyace commentedon Nov 21, 2018
@guardrex - thanks! Can you point me to any sample on how the validation for IOptions works.
guardrex commentedon Nov 23, 2018
@charinirao I'm working on the PR for this update now. Turns out we did cover validation ... it's in the 2.2 version of the topic. The 2.2 version will become the default version of the topic after 2.2 RTM.
See: https://docs.microsoft.com/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.2#options-validation
abakumov-v commentedon Nov 28, 2018
Hi guys! It’s not quite clear to me why validation of options is needed, since it is possible in your Ioc container to register dependency resolution for your
IOptions<>- I'm found that this case is work fine:In my opinion, options validation is the developer responsibility, and if developer does not describe validation rules (in Ioc, for example), then it becomes a problem of developer :)
May be I'm wrong - tell me, please, if this is so.