我有这堂课:
internal class UserRepository : Repository<User>, IUserPasswordStore<User>, IUserLoginStore<User>
{
public UserRepository(DbContext context)
: base(context)
{
}
public Task<string> GetPasswordHashAsync(User user)
{
if (user == null)
throw new ArgumentNullException("user");
return Task.FromResult<string>(user.PasswordHash);
}
public Task<bool> HasPasswordAsync(User user)
{
return Task.FromResult<bool>(user.PasswordHash != null);
}
public Task SetPasswordHashAsync(User user, string passwordHash)
{
if (user == null)
throw new ArgumentNullException("user");
user.PasswordHash = passwordHash;
return Task.FromResult<int>(0);
}
public Task AddLoginAsync(User user, UserLoginInfo login)
{
throw new NotImplementedException();
}
public Task<User> FindAsync(UserLoginInfo login)
{
throw new NotImplementedException();
}
public Task<IList<UserLoginInfo>> GetLoginsAsync(User user)
{
throw new NotImplementedException();
}
public Task RemoveLoginAsync(User user, UserLoginInfo login)
{
throw new NotImplementedException();
}
#region NotUsed
public void Dispose()
{
throw new NotImplementedException();
}
public Task CreateAsync(User user)
{
throw new NotImplementedException();
}
public Task DeleteAsync(User user)
{
throw new NotImplementedException();
}
public Task<User> FindByIdAsync(string userId)
{
throw new NotImplementedException();
}
public Task<User> FindByNameAsync(string userName)
{
throw new NotImplementedException();
}
public Task UpdateAsync(User user)
{
throw new NotImplementedException();
}
#endregion
}
在我的服务中我有这个:
public class UserService : Service<User>
{
private readonly string companyId;
public IPasswordHasher PasswordHasher { get; set; }
public IIdentityValidator<string> PasswordValidator { get; set; }
public IIdentityValidator<User> UserValidator { get; set; }
public ClaimsIdentityFactory ClaimsIdentityFactory { get; set; }
public virtual bool SupportsUserSecurityStamp { get { return this.Repository is IUserSecurityStampStore<User>; } }
public UserService(IUnitOfWork uow, string companyId)
: base (uow)
{
this.PasswordHasher = new PasswordHasher();
this.UserValidator = new UserValidator(this);
this.PasswordValidator = new MinimumLengthValidator(6);
this.ClaimsIdentityFactory = new ClaimsIdentityFactory();
this.companyId = companyId;
}
private IUserPasswordStore<User> GetPasswordStore()
{
var userPasswordStore = this.Repository as IUserPasswordStore<User>;
if (userPasswordStore == null)
throw new NotSupportedException(Resources.StoreNotIUserPasswordStore);
return userPasswordStore;
}
}
GetPasswordStore 是问题所在。 userPasswordStore 始终为null,即使存储库正在实现它。
为清楚起见,这是服务类:
public class Service<T> where T : class
{
private readonly IRepository<T> repository;
protected IRepository<T> Repository
{
get { return this.repository; }
}
internal Service(IUnitOfWork unitOfWork)
{
this.repository = unitOfWork.GetRepository<T>();
}
}
任何人都可以将 UserRepository 作为 IUserPasswordStore 返回null吗? 我会盲目地盯着它看:)