我正在努力熟悉OWIN,有很多事情让我感到困惑。例如,在部分startup.cs类中,我通过注册上下文回调
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
有什么区别?为什么我们需要通用方法?
我可以得到这样的上下文:
context.Get<ApplicationDbContext>())
context.GetUserManager<ApplicationUserManager>()
Get和GetUserManager方法之间有什么区别?为什么我不能直接调用上下文。获取ApplicationUserManager?
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>();
}