4

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?

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Toddams
  • 1,489
  • 15
  • 23

1 Answers1

6

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>();
    }
jamesSampica
  • 12,230
  • 3
  • 63
  • 85
  • Doesn't answer the Q, *what's the difference between Get() and the generic version*. – Anders Abel Feb 18 '15 at 19:10
  • 1
    So if there is no difference, I can easily call context.Get() instead of context.GetUserManager? So why do we need a separate method for only getting a UserManager? – Toddams Feb 18 '15 at 21:37
  • Yes. You dont need a separate method and you can obviously interchange them. As for the reasoning for this I'm not sure. Could be something left over from an earlier version of Identity. – jamesSampica Feb 18 '15 at 22:09
  • @Shoe is `Get` not part of Owin? and `GetUserManager` is from Identity? I mean they are from different assemblies and used by different components? – trailmax Feb 18 '15 at 22:51
  • Thank you, finally I got it ;) – Toddams Feb 18 '15 at 23:29
  • 2
    @trailmax `Get(string)` is part of OWIN `Get()` and `GetUserManager()` are some owin extensions in the identity namespace. – jamesSampica Feb 19 '15 at 00:06
  • @Shoe in that case having 2 methods make sense. I'd include that in the answer as a reason behind having 2 similar methods. – trailmax Feb 19 '15 at 00:42
  • @trailmax The two `Get` methods make sense. I'm not sure why `GetUserManager` exists – jamesSampica Feb 19 '15 at 04:03