我正在构建所有业务逻辑从控制器分开到单独lib的应用程序。我想从控制器中访问ApplicationUserManager
,而无需在那里访问Request
/OwinContext
。是否可以?如果是,使用di容器注册ApplicationUserManager
的更好方法是什么?(autoFac,ninject等(
如果要分开业务逻辑,则可以使用UserStore
和UserManager
。我总是喜欢Unity依赖注入。
示例:
public class UserRepository
{
private readonly UserStore _store;
private readonly UserManager _manager;
public UserRepository(DbContext context)
{
_store = new UserStore<ApplicationUser>(context);
_manager = new UserManager<ApplicationUser>(_store);
}
public async Task<ApplicationUser> GetUserByNameAsync(string username)
{
return await _store.FindByNameAsync(username);
}
public async Task<IEnumerable<ApplicationUser>> GetAllUsersAsync()
{
return await _store.Users.ToArrayAsync();
}
public async Task CreateAsync(ApplicationUser user, string password)
{
await _manager.CreateAsync(user, password);
}
public async Task DeleteAsync(ApplicationUser user)
{
await _manager.DeleteAsync(user);
}
public async Task UpdateAsync(ApplicationUser user)
{
await _manager.UpdateAsync(user);
}
private bool _disposed = false;
public void Dispose()
{
if (!_disposed)
{
_manager.Dispose();
_store.Dispose();
}
_disposed = true;
}
}