我有这段代码——
public class UserManager : UserManager<ApplicationUser>
{
private ApplicationDbContext _dbAccess;
public UserManager() :
base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
{
this.UserValidator = new CustomUserValidator<ApplicationUser>(this);
var provider = new MachineKeyProtectionProvider();
this.UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser>(
provider.Create("SomeCoolAuthentication"));
//DO I REALLY NEED TO DO THIS AGAIN?
this._dbAccess = new ApplicationDBContext();
}
public bool myOwnHelperMethod(){
//is there a way to use the ApplicationDbContext instance that
//was initialized in the base constructor here?
//Or do i have to create a new instance?
}
}
有没有更好的方法来编写它,以便我可以实例化 ApplicationDBContext,使用它来调用基本构造函数,然后稍后在一些辅助方法中使用相同的实例?或者我是否必须在构造函数中创建另一个实例以在辅助方法中使用。