Asp.net mvc 向AccountController的Simpleinjector注册Identity Framework UserStore

Asp.net mvc 向AccountController的Simpleinjector注册Identity Framework UserStore,asp.net-mvc,asp.net-mvc-4,dependency-injection,asp.net-identity,simple-injector,Asp.net Mvc,Asp.net Mvc 4,Dependency Injection,Asp.net Identity,Simple Injector,我想使用Identity framework中的自定义UserStore。我的控制器看起来像这样 public AccountController() : this(new UserManager<ApplicationUser>(new MyUserStore())) { } public AccountController(UserManager<ApplicationUser> userManager) { UserManager = userManag

我想使用Identity framework中的自定义UserStore。我的控制器看起来像这样

public AccountController()
: this(new UserManager<ApplicationUser>(new MyUserStore()))
{
}
public AccountController(UserManager<ApplicationUser> userManager)
{
      UserManager = userManager;
}
publiccountcontroller()
:此(新用户管理器(新MyUserStore()))
{
}
公共帐户控制器(用户管理器用户管理器)
{
UserManager=UserManager;
}

现在Simpleinjector不允许多个构造函数。它可以允许只使用一个构造函数注册这些类。在这种情况下,我该如何解决这个问题呢。使用Simpleinjector注册

简单注入器需要一个公共构造函数。如果您有多个,它无法确定在运行时使用哪一个

解决方案是一个设计问题-为什么需要多个构造函数?这可以通过问你自己来回答

我是希望SimpleInjector创建IUserStore的实例,还是手动创建

我假设它是前者,因为如果不允许注入依赖项,那么AccountController就很难测试

以下配置(通常存储在App_Start/SimpleInjectorInitializer.cs中)将允许IUserStore实例为您的自定义类型:MyUserStore,并且如果构造函数需要,将注入IUserManager中

container.Register<IUserStore, MyUserStore>();    
container.Register<IUserManager<ApplicationUser>, UserManager<ApplicationUser>>();
container.Register();
container.Register();

在Baldy和Steven的建议下,我最终得到了如下解决方案:

public class ApplicationUserManager : UserManager<Users, Guid>
{
    public ApplicationUserManager(IUserStore<Users, Guid> Store)
        : base(Store)
    {        
    }
}

public class ApplicationUserStore 
    : UserStore<Users, Roles, Guid, UserLogins, UserRoles, Claims>,
    IUserStore<Users,Guid>
{
    public ApplicationUserStore(RemsContext Context)
        : base(Context)
    {          
    }
}

如果方法正确,我想邀请@Steven和@Baldy对此发表评论

+1。回答得很好。请注意,如果需要将多个版本的
IUserManager
映射到
UserManager
,也可以执行
container.RegisterOpenGeneric(typeof(IUserManager),typeof(UserManager))
。为此,您需要包含
SimpleInjector.Extensions
命名空间。防止有多个构造函数:这是一个问题。@Steven如果这个方法看起来不错,请对其进行评论。我已经遵循了Baldy的建议,但是在这里对上下文进行了一些小的调整。很难说在这方面有多大用处。您似乎正确地应用了,并且只直接注入您需要的内容,这很好。然而,我有点担心您的基类
UserManager
UserStore
。很难说,但是您可能在这些基类中违反了and。如果您有疑问,请在上提问,并在评论中添加链接。按照Steven已经提到的内容,您的应用程序商店看起来相当“繁忙”,这可能表示违反了SRP。这可能只是因为您尚未发现如何将OpenGenerics与SimpleInjector一起使用。请阅读这里的相关内容——如果OpenGenerics不适用于此,那么可能值得研究ApplicationUserStore的角色,看看它的职责是否可以分解。这个类的设计可能最好作为它自己的问题来处理
container.RegisterPerWebRequest<DbContext, RemsContext>();

container.Register<UserManager<Users, Guid>, ApplicationUserManager>();
container.Register<RoleManager<Roles, Guid>, ApplicationRoleManager>();

container.Register<IUserStore<Users, Guid>, 
    UserStore<Users, Roles, Guid, UserLogins, UserRoles, Claims>>();
container.Register<IRoleStore<Roles, Guid>, RoleStore<Roles, Guid, UserRoles>>();
public AccountsController(ApplicationUserManager _userManager,
    ApplicationRoleManager _roleManager)
{
    this.UserManager = _userManager;           
    this.RoleManager = _roleManager;
}