context.Get()及其通用版本之间的区别是什么?

时间:2015-02-18 14:07:40

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

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

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

区别是什么?为什么我们需要这种通用方法?

我可以像这样得到这样的背景:

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

Get和GetUserManager方法之间的区别是什么?为什么我不能只调用context.Get for ApplicationUserManager?

1 个答案:

答案 0 :(得分:6)

Get<UserManager>GetUserManager<UserManager>

之间没有区别

这是两个源代码......

    /// <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>();
    }