【问题标题】:What is IOptions in AspNetCore Identity 2.0?AspNetCore Identity 2.0 中的 IOptions 是什么?
【发布时间】:2018-01-30 09:30:52
【问题描述】:

我试图了解身份是如何结合在一起的。我想实现 Dapper 而不是 Entity Framework,但我觉得为了让我能够在未来提供支持,我需要了解发生了什么。

因此,我从 Identity Repository 下载了 2.0.0 版本:https://github.com/aspnet/Identity/releases 并开始四处寻找。

我现在卡住的部分是IOptions<IdentityOptions>,它需要由 UserManager 传入。查看IdentityOptions 类本身,我知道这些是用于诸如 PasswordOptions 等的配置。所以我需要实例化并设置它们,然后绑定它们。

但是,我不明白的部分是IOptions 接口。它似乎是一个包装器,但它为什么在那里,它的目的是什么?为什么不自己创建IdentityOptions,为什么要包裹在IOptions接口里面呢?

【问题讨论】:

标签: c# asp.net-identity asp.net-core-2.0 asp.net-core-identity


【解决方案1】:

据我了解,IOptions 是检索任何已配置对象及其基础属性的接口。例如,您在 IdentityOptions 类下有多个 Options 对象。通过将选项类添加到服务集合对象中,您可以将 IdentityOptions 插入 DI 管道,而不是一一实例化它们。这样,您可以通过构造函数注入在任何地方调用访问任何 Options 对象(来自 IdentityOptions 类)。

要记住的一点是,IdentityOptions 下的所有“Options”对象本身就是类。因此,为避免所有手动实例化,您可以通过将 IOptions 包裹在 Options 类周围来使用 DI 管道。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2022-11-09
    • 2019-02-04
    • 2014-03-05
    相关资源
    最近更新 更多