context.Get()和它的通用版本之间有什么区别?

Tod*_*ams 4 asp.net owin katana asp.net-identity

我正在努力熟悉OWIN,有很多让我感到困惑的事情.例如,在部分startup.cs类中,我通过注册上下文回调

app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
Run Code Online (Sandbox Code Playgroud)

有什么不同?为什么我们需要这种通用方法?

我可以得到这样的上下文:

context.Get<ApplicationDbContext>())
context.GetUserManager<ApplicationUserManager>()
Run Code Online (Sandbox Code Playgroud)

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

Sho*_*hoe 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>();
    }
Run Code Online (Sandbox Code Playgroud)

  • @trailmax`Get <T>(string)`是OWIN的一部分`Get <T>()`和`GetUserManager <T>()`是身份命名空间中的一些owin扩展. (2认同)