Microsoft练习使用Unity Register Type

时间:2014-05-22 14:42:55

标签: c# dependency-injection asp.net-mvc-5 unity-container

我有几个场景,我很难映射这些类型。

情景1:

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

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

类型定义

 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
    {
....
  }

尝试映射

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

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

错误:无法将“Main.Models.ApplicationUser”类型的对象强制转换为“Microsoft.AspNet.Identity.IUserStore 1[Main.Models.ApplicationUser]'. With out mapping, i get this error: The current type, Microsoft.AspNet.Identity.IUserStore 1 [Main.Models.ApplicationUser],是一个接口而不能被建造。你错过了类型映射吗?

情景2:

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

类型定义

 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; }
        }
    }

尝试映射:

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

错误:没有错误。但我总是在handySession.Access中有null

2 个答案:

答案 0 :(得分:11)

感谢emodendroket帮助我解决了Entity Framework的下一个映射问题。

但是对于我的场景,我可以用两种方式映射类型

第一个

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

第二个

 container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
            container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
            container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());

答案 1 :(得分:2)

如果你想在MVC5中统一IOC,请遵循这些要点

  1. 创建一个新项目asp.net mvc 5(非空)
  2. 使用unity =&gt; install-package unity.mvc
  3. WIF(Windows身份框架)在应用程序中被破坏
  4. 按照这个优秀的教程
  5. you need to inject all identity framework entities
相关问题