[英]How to use DI with UserManager and UserStore
Given a typical setup of a MVC controller constructor passing UserManager
(which takes UserStore
) to it's parent class, how would this be converted to be injected via IoC? 给定一个MVC控制器构造函数的典型设置,它将
UserManager
(需要UserStore
)传递给它的父类,如何将其转换为通过IoC注入?
Starting with this: 从此开始:
public AccountController()
: this(new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}
I would think something like this: 我会这样想:
public AccountController(IUserStore store)
: this(new UserManager<ApplicationUser>(store)))
{
}
Though this does, of course, lose the IdentityDbContext
. 尽管这样做的确会丢失
IdentityDbContext
。
How should the IoC be setup and how should the constructor be defined to allow injection of the UserManager, UserStore and IdentityDbContext? 应该如何设置IoC以及如何定义构造函数以允许注入UserManager,UserStore和IdentityDbContext?
You would need to create some classes to allow for easier injection. 您将需要创建一些类以简化注入。
Let us start with the UserStore. 让我们从UserStore开始。 Create the desired interface and have it inherit from
IUserStore<ApplicationUser>
创建所需的接口并使其继承自
IUserStore<ApplicationUser>
public IUserStore : IUserStore<ApplicationUser> { }
Create an implementation as follows. 创建一个如下的实现。
public ApplicationUserStore : UserStore<ApplicationUser>, IUserSTore {
public ApplicationUserStore(ApplicationDbContext dbContext)
:base(dbContext) { }
}
The UserManager can then be done as desired in the OP. 然后可以根据需要在OP中完成UserManager。
public class ApplicationUserManager : UserManager<ApplicationUser> {
public ApplicationUserManager(IUserSTore userStore) : base(userStore) { }
}
SO now all that is left is to make sure that which ever IoC container you decide to use registers the necessary classes. 因此,现在剩下的就是确保您决定使用哪个IoC容器注册必要的类。
ApplicationDbContext --> ApplicationDbContext
IUserStore --> ApplicationUserStore
If you want to go a step further and abstract the UserManager then just create an interface that exposes the functionality you want 如果您想更进一步并抽象化UserManager,则只需创建一个接口即可,该接口公开了您想要的功能
public interface IUserManager<TUser, TKey> : IDisposable
where TUser : class, Microsoft.AspNet.Identity.IUser<TKey>
where TKey : System.IEquatable<TKey> {
//...include all the properties and methods to be exposed
IQueryable<TUser> Users { get; }
Task<TUser> FindByEmailAsync(string email);
Task<TUser> FindByIdAsync(TKey userId);
//...other code removed for brevity
}
public IUserManager<TUser> : IUserManager<TUser, string>
where TUser : class, Microsoft.AspNet.Identity.IUser<string> { }
public IApplicationUserManager : IUserManager<ApplicationUser> { }
and have you manager inherit from that. 并请经理从中继承。
public class ApplicationUserManager : UserManager<ApplicationUser>, IApplicationUserManager {
public ApplicationUserManager(IUserSTore userStore) : base(userStore) { }
}
This now means that the Controller can now depend on an abstraction and not on implementation concerns 现在,这意味着Controller现在可以依赖抽象而不是实现问题了
private readonly IApplicationUserManager userManager;
public AccountController(IApplicationUserManager userManager) {
this.userManager = userManager;
}
And again you register the interface with the implementation in the IoC container. 再次在IoC容器中向实现注册接口。
IApplicationUserManager --> ApplicationUserManager
UPDATE: 更新:
If you are feeling adventurous and want to abstract identity framework itself take a look at the answer given here 如果您喜欢冒险并且想要抽象身份框架本身,请查看此处给出的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.