How to use DI with UserManager and UserStore

Given the typical setup of the MVC controller constructor passing the UserManager (which accepts the UserStore ) to this parent class, how can this be converted for input through IoC?

Starting from 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))) { } 

Although this, of course, will lose IdentityDbContext .

How to configure IoC and how should the constructor be defined to allow embedding of UserManager, UserStore and IdentityDbContext?

+3
source share
1 answer

You will need to create several classes to simplify the injection process.

Let's start with UserStore. Create the desired interface and inherit it from IUserStore<ApplicationUser>

 public IUserStore : IUserStore<ApplicationUser> { } 

Create an implementation as follows.

 public ApplicationUserStore : UserStore<ApplicationUser>, IUserSTore { public ApplicationUserStore(ApplicationDbContext dbContext) :base(dbContext) { } } 

Then the user manager can be executed as desired in the OP.

 public class ApplicationUserManager : UserManager<ApplicationUser> { public ApplicationUserManager(IUserSTore userStore) : base(userStore) { } } 

SO now all that remains is to make sure that in any IoC container you decide to use registers for the required classes.

 ApplicationDbContext --> ApplicationDbContext IUserStore --> ApplicationUserStore 

If you want to take a step further and distract the UserManager, then just create an interface that provides the necessary functions

 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 you have a manager inherited.

 public class ApplicationUserManager : UserManager<ApplicationUser>, IApplicationUserManager { public ApplicationUserManager(IUserSTore userStore) : base(userStore) { } } 

Now this means that now the controller may depend on abstraction, and not on implementation problems

 private readonly IApplicationUserManager userManager; public AccountController(IApplicationUserManager userManager) { this.userManager = userManager; } 

And again, you register the implementation interface in the IoC container.

 IApplicationUserManager --> ApplicationUserManager 

UPDATE:

If you feel adventurous and want to distract the very framework of identification, look at the answer provided here

+3
source

Source: https://habr.com/ru/post/1273465/


All Articles