【问题标题】:What's the difference between context.Get() and its generic version?context.Get() 和它的通用版本有什么区别?
【发布时间】:2015-02-19 05:22:44
【问题描述】:

我正在尝试熟悉 OWIN,但有很多事情让我感到困惑。例如,在部分 startup.cs 类中,我通过

注册上下文回调
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

有什么区别?为什么我们需要那个泛型方法?

我可以像这样获得上下文:

context.Get<ApplicationDbContext>())
context.GetUserManager<ApplicationUserManager>()

Get 和 GetUserManager 方法有什么区别?为什么我不能只为 ApplicationUserManager 调用 context.Get?

【问题讨论】:

  • 什么是context? dbContext、OWIN 上下文、HttpContext?
  • Owin context,我觉得很明显

标签: asp.net asp.net-identity owin katana


【解决方案1】:

Get&lt;UserManager&gt;GetUserManager&lt;UserManager&gt;没有区别

这是两者的源代码...

    /// <summary>
    ///     Retrieves an object from the OwinContext using a key based on the AssemblyQualified type name
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="context"></param>
    /// <returns></returns>
    public static T Get<T>(this IOwinContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        return context.Get<T>(GetKey(typeof (T)));
    }

    /// <summary>
    ///     Get the user manager from the context
    /// </summary>
    /// <typeparam name="TManager"></typeparam>
    /// <param name="context"></param>
    /// <returns></returns>
    public static TManager GetUserManager<TManager>(this IOwinContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        return context.Get<TManager>();
    }

【讨论】:

  • 不回答问题,Get() 和通用版本有什么区别
  • 所以如果没有区别,我可以轻松调用 context.Get() 而不是 context.GetUserManager?那么为什么我们需要一个单独的方法来只获取一个 UserManager 呢?
  • 是的。您不需要单独的方法,显然可以互换它们。至于这样做的原因我不确定。可能是早期版本的 Identity 遗留下来的东西。
  • @Shoe 是 Get&lt;T&gt; 不是 Owin 的一部分吗?而GetUserManager 来自身份?我的意思是它们来自不同的程序集并由不同的组件使用?
  • @trailmax Get&lt;T&gt;(string) 是 OWIN 的一部分 Get&lt;T&gt;()GetUserManager&lt;T&gt;() 是身份命名空间中的一些 owin 扩展。
猜你喜欢
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2015-03-17
  • 1970-01-01
  • 2010-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多