Search code examples
asp.netasp.net-identityowinkatana

What's the difference between context.Get() and its generic version?


I'm trying to get familiar with OWIN and there are a lot of things that confuse me. For example, in partial startup.cs class I register context callback via

app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

What's the difference? Why do we need that generic method?

I can get that context like this:

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

What's the difference between the Get and GetUserManager methods? Why can't I just call context.Get for ApplicationUserManager?


Solution

  • There is no difference between Get<UserManager> and GetUserManager<UserManager>

    Here's the source code for both...

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