Skip to content

Why should we use IOptions<T> when we can inject the configuration object directly. #9663

Closed

Description

@suwyace

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.

Activity

  1. added
    Source - Docs.ms
    on Nov 21, 2018
  2. guardrex commented on Nov 21, 2018

    @guardrex
    Collaborator

    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?

  3. HaoK commented on Nov 21, 2018

    @HaoK
    Member

    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

  4. guardrex commented on Nov 21, 2018

    @guardrex
    Collaborator

    Thanks ... I'll see about getting that in along with the work for #9662.

  5. added and removed on Nov 21, 2018
  6. added this to the 2018 Q 4 ends Dec 31 milestone on Nov 21, 2018
  7. suwyace commented on Nov 21, 2018

    @suwyace
    Author

    @guardrex - thanks! Can you point me to any sample on how the validation for IOptions works.

  8. self-assigned this
    on Nov 22, 2018
  9. guardrex commented on Nov 23, 2018

    @guardrex
    Collaborator

    @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

  10. abakumov-v commented on Nov 28, 2018

    @abakumov-v

    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:

            public static void RegisterAppSettings(this ContainerBuilder builder)
            {
                builder.Register(context =>
                {
                    var appSettingsOptions = context.Resolve<IOptions<AppSettings>>();
                    if (appSettingsOptions?.Value == null)
                        throw new ArgumentNullException(nameof(appSettingsOptions));
    
                    if (appSettingsOptions.Value.ConnectionStrings == null)
                        throw new ArgumentNullException(nameof(appSettingsOptions),
                            "Section \"ConnectionStrings\" in configuration file is null");
    
                    if (appSettingsOptions.Value.MailSettings == null)
                        throw new ArgumentNullException(nameof(appSettingsOptions),
                            "Section \"MailSettings\" in configuration file is null");
    
                    return appSettingsOptions.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Pri2Source - Docs.ms

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions