public interface IUserRepository : IBaseRepository<user>
{
user GetUser(int userId);
user Get(string Email);
}
public class UserRepository : BaseRepository<user>, IUserRepository
{
public UserRepository(IUnitOfWork unit) : base(unit)
{
}
public void Dispose()
{
throw new NotImplementedException();
}
public user GetUser(int userId)
{
return dbSet.Where(x => x.ID == userId).FirstOrDefault();
}
public user Get(string Email)
{
var obj = dbSet.Where(s => s.Email == Email).FirstOrDefault();
return obj;
}
}
我正在我的控制器中使用存储库,如下所示
public class AccountController : Controller
{
private readonly ApplicationUserManager UserManager;
private readonly ApplicationSignInManager SignInManager;
private readonly IAuthenticationManager AuthenticationManager;
private readonly IUnitOfWork uow;
private readonly UserRepository userrepo;
public AccountController(UserRepository _userrepo, ApplicationUserManager userManager, ApplicationSignInManager signInManager, IAuthenticationManager authenticationManager,IUnitOfWork _uow)
{
this.UserManager = userManager;
this.SignInManager = signInManager;
this.AuthenticationManager = authenticationManager;
this.uow = _uow;
userrepo = _userrepo;
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid )
{
var user = UserManager.FindByEmail(model.Email);
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
if (result)
{
var myUser = userRepo.Get(user.Id);
if (myUser.SubscriptionStatus == 1)
{
return RedirectToAction("ChangePassword", "Manage", new { ReturnUrl = returnUrl });
}
else
{
return RedirectToAction("Index","Admin");
}
}
}
}
这是我的动作过滤器
public class CheckFirstLoginAttribute : ActionFilterAttribute
{
private readonly ApplicationUserManager UserManager;
private readonly IUnitOfWork uow;
private readonly UserRepository userrepo;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
string uName = HttpContext.Current.User.Identity.Name;
if (!string.IsNullOrEmpty(uName))
{
//var user = UserManager.FindByEmailAsync(uName);
//The above & below lines are not creating instance of the UserManager & UserRepository object, it is always null
user cUser= userrepo.GetUserId(uName);
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
{
controller = "Manage",
action = "ChangePassword"
}));
}
}
}
我正在使用Unity进行依赖注入
public class UnityConfig
{
#region Unity Container
private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
});
public static IUnityContainer GetConfiguredContainer()
{
return container.Value;
}
#endregion
private static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<MyDbContext>();
container.RegisterType<UserRepository>();
container.RegisterType<IUnitOfWork, UnitOfWork>();
container.RegisterType<ApplicationDbContext>();
container.RegisterType<ApplicationSignInManager>();
container.RegisterType<ApplicationUserManager>();
container.RegisterType<HomeController>();
container.RegisterType<AccessCodeController>();
container.RegisterType<AdminController>();
container.RegisterType<IAuthenticationManager>(
new InjectionFactory(c => HttpContext.Current.GetOwinContext().Authentication));
container.RegisterType<IUserStore<MyUser, int>, UserStore<MyUser, MyRole, int, MyUserLogin, MyUserRole, MyUserClaim>>(
new InjectionConstructor(typeof(ApplicationDbContext)));
}
}
如何创建存储库类的实例并访问Get(字符串电子邮件)方法。这样我就可以从数据库中检查订阅状态。
我尝试了很多方法,但总是无法创建实例。
请帮助我。
由于 塔拉克
答案 0 :(得分:0)
public class CheckFirstLoginAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
string uName = HttpContext.Current.User.Identity.Name;
if (!string.IsNullOrEmpty(uName))
{
//Here is how I could get the instance of the repository
//I had to register the repository type and then
//I resolved it to obtain the object of the repository and
//access the methods in the repository
var container = new UnityContainer();
container.RegisterType<IUnitOfWork, UnitOfWork>();
container.RegisterType<UserRepository>();
UserRepository repo = container.Resolve<UserRepository>();
user cUser = repo.GetUserId(uName);
if (cUser.SubscriptionStatus == 1)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
{
controller = "Manage",
action = "ChangePassword"
}));
}
}
}
}