title: Microsoft Practice using Unity Register Type
body: <p>I have couple of scenarios where I am having hard time to map the types.</p>
  
  <p>Scenario 1:</p>
  
  <pre><code> public AccountController()
              : this(new UserManager&lt;ApplicationUser&gt;(new UserStore&lt;ApplicationUser&gt;(new ApplicationDbContext())))
          {
          }
  
          public AccountController(UserManager&lt;ApplicationUser&gt; userManager)
          {
              UserManager = userManager;
          }
  </code></pre>
  
  <p>Type Definition</p>
  
  <pre><code> public class ApplicationUser : IdentityUser
      {
      }
  
   // Summary:
      //     Implements IUserStore using EntityFramework where TUser is the entity type
      //     of the user being stored
      //
      // Type parameters:
      //   TUser:
      public class UserStore&lt;TUser&gt; : IUserLoginStore&lt;TUser&gt;, IUserClaimStore&lt;TUser&gt;, IUserRoleStore&lt;TUser&gt;, IUserPasswordStore&lt;TUser&gt;, IUserSecurityStampStore&lt;TUser&gt;, IUserStore&lt;TUser&gt;, IDisposable where TUser : global::Microsoft.AspNet.Identity.EntityFramework.IdentityUser
      {
   ....
    }
  
  public class UserManager&lt;TUser&gt; : IDisposable where TUser : global::Microsoft.AspNet.Identity.IUser
      {
  ....
    }
  </code></pre>
  
  <p>Attempted Mapping</p>
  
  <pre><code>  container.RegisterType(typeof(IUserStore&lt;&gt;), typeof(ApplicationUser));
  
              container.RegisterType(typeof(IDisposable), typeof(UserManager&lt;&gt;));
  </code></pre>
  
  <p><strong>Error: Unable to cast object of type 'Main.Models.ApplicationUser' to type 'Microsoft.AspNet.Identity.IUserStore<code>1[Main.Models.ApplicationUser]'.
  With out mapping, i get this error: The current type, Microsoft.AspNet.Identity.IUserStore</code>1[Main.Models.ApplicationUser], is an interface and cannot be constructed. Are you missing a type mapping?</strong></p>
  
  <p>Scenario 2:</p>
  
  <pre><code> public RegisterController(
               IUserDataStorage&lt;HandyGuy&gt; session)
          {
              this.handySession = session;
          }
  </code></pre>
  
  <p>Type Definition</p>
  
  <pre><code> public class HttpUserDataStorage&lt;T&gt; : IUserDataStorage&lt;T&gt;
    where T : class
      {
          public T Access
          {
              get { return HttpContext.Current.Session[typeof(T).FullName] as T; }
              set { HttpContext.Current.Session[typeof(T).FullName] = value; }
          }
      }
  </code></pre>
  
  <p>Attempted Mapping:</p>
  
  <pre><code> container.RegisterType(typeof(IUserDataStorage&lt;&gt;), typeof(HttpUserDataStorage&lt;&gt;));
  </code></pre>
  
  <p><strong>Error: No error. But I always have null in handySession.Access</strong></p>