C# 使用统一寄存器类型的Microsoft实践
我有两个场景,我很难映射类型 情景1:C# 使用统一寄存器类型的Microsoft实践,c#,dependency-injection,asp.net-mvc-5,unity-container,C#,Dependency Injection,Asp.net Mvc 5,Unity Container,我有两个场景,我很难映射类型 情景1: public AccountController() : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()))) { } public AccountController(UserManager<Applicat
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
{
....
}
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; }
}
}
公共类HttpUserDataStorage:IUserDataStorage
T:在哪里上课
{
公共电话接入
{
获取{return HttpContext.Current.Session[typeof(T).FullName]作为T;}
set{HttpContext.Current.Session[typeof(T).FullName]=value;}
}
}
尝试映射:
container.RegisterType(typeof(IUserDataStorage<>), typeof(HttpUserDataStorage<>));
container.RegisterType(typeof(IUserDataStorage),typeof(HttpUserDataStorage));
错误:无错误。但是我的handySession中总是有null。Access这帮助我解决了实体框架的下一个映射问题
但是对于我的场景,我可以用两种方式映射类型
第一个
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(typeof(UserManager),
新的注入构造函数(typeof(IUserStore));
RegisterType(新的InjectionFactory(c=>c.Resolve());
RegisterType(typeof(IUserStore),typeof(UserStore));
RegisterType(新的ContainerControlledLifetimeManager());
RegisterType(新的ContainerControlledLifetimeManager());
第二个
container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());
container.RegisterType(新的层次结构CallifetimeManager());
RegisterType(新的层次结构CallifetimeManager());
RegisterType(新的层次结构CallifetimeManager());
如果您想在MVC5中使用unity IOC,请遵循以下几点
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());