【发布时间】:2018-03-10 16:54:01
【问题描述】:
我有这个sn-p的代码--
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,使用它来调用基本构造函数,然后稍后在一些辅助方法中使用相同的实例?还是我必须在构造函数中创建另一个实例以用于辅助方法。
【问题讨论】:
-
@S.Akbari 请解释您提出该建议的原因。这看起来是一个很好的 Stack Overflow 问题。
-
@200_success 有没有更好的写法?
-
@S.Akbari 请查看Code Review help center。注意到什么了吗?示例代码不可接受。
标签: c# class-constructors