Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft Practice using Unity Register Type

I have couple of scenarios where I am having hard time to map the types.

Scenario 1:

 public AccountController()
            : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
        {
        }

        public AccountController(UserManager<ApplicationUser> userManager)
        {
            UserManager = userManager;
        }

Type Definition

 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<TUser> : IUserLoginStore<TUser>, IUserClaimStore<TUser>, IUserRoleStore<TUser>, IUserPasswordStore<TUser>, IUserSecurityStampStore<TUser>, IUserStore<TUser>, IDisposable where TUser : global::Microsoft.AspNet.Identity.EntityFramework.IdentityUser
    {
 ....
  }

public class UserManager<TUser> : IDisposable where TUser : global::Microsoft.AspNet.Identity.IUser
    {
....
  }

Attempted Mapping

  container.RegisterType(typeof(IUserStore<>), typeof(ApplicationUser));

            container.RegisterType(typeof(IDisposable), typeof(UserManager<>));

Error: Unable to cast object of type 'Main.Models.ApplicationUser' to type 'Microsoft.AspNet.Identity.IUserStore1[Main.Models.ApplicationUser]'. With out mapping, i get this error: The current type, Microsoft.AspNet.Identity.IUserStore1[Main.Models.ApplicationUser], is an interface and cannot be constructed. Are you missing a type mapping?

Scenario 2:

 public RegisterController(
             IUserDataStorage<HandyGuy> session)
        {
            this.handySession = session;
        }

Type Definition

 public class HttpUserDataStorage<T> : IUserDataStorage<T>
  where T : class
    {
        public T Access
        {
            get { return HttpContext.Current.Session[typeof(T).FullName] as T; }
            set { HttpContext.Current.Session[typeof(T).FullName] = value; }
        }
    }

Attempted Mapping:

 container.RegisterType(typeof(IUserDataStorage<>), typeof(HttpUserDataStorage<>));

Error: No error. But I always have null in handySession.Access

like image 338
HaBo Avatar asked Dec 12 '22 05:12

HaBo


1 Answers

Thanks to emodendroket that helped me to resolve the next mapping issue with Entity Framework.

But For my scenario, I could map the types in two ways

First one

 container.RegisterType(typeof(UserManager<>),
            new InjectionConstructor(typeof(IUserStore<>)));
            container.RegisterType<Microsoft.AspNet.Identity.IUser>(new InjectionFactory(c => c.Resolve<Microsoft.AspNet.Identity.IUser>()));
            container.RegisterType(typeof(IUserStore<>), typeof(UserStore<>));
            container.RegisterType<IdentityUser, ApplicationUser>(new ContainerControlledLifetimeManager());
            container.RegisterType<DbContext, ApplicationDbContext>(new ContainerControlledLifetimeManager());

second one

 container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
            container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
            container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());
like image 161
HaBo Avatar answered Dec 20 '22 17:12

HaBo