身份验证类应该是静态的还是从实例创建?
身份验证类应该是静态的还是从实例创建?
我遇到的问题是,如果不在任何控制器中注入身份验证实例,我就无法访问身份验证,这有点问题。
public class HomeController : Controller
{
//=> I WANT TO AVOID THIS! <=
//protected IAuthentification Authentification { get; private set; }
//public HomeController(IAuthentification authentification)
//{
// Authentification = authentification;
//}
public ActionResult Index()
{
//Authentification.CreerUtilisateur(new Utilisateur { Nom = "Test", NomUsage = "ttt", MotDePasse = "ddsss" });
return View();
}
}
Should an authentication class be static or created from an instance ?
The problem I have is that I cannot access the authentication without injecting an instance of it in any controllers and that a bit problematic.
public class HomeController : Controller
{
//=> I WANT TO AVOID THIS! <=
//protected IAuthentification Authentification { get; private set; }
//public HomeController(IAuthentification authentification)
//{
// Authentification = authentification;
//}
public ActionResult Index()
{
//Authentification.CreerUtilisateur(new Utilisateur { Nom = "Test", NomUsage = "ttt", MotDePasse = "ddsss" });
return View();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

问题是什么?什么是‘有问题’?构造函数注入是值得付出努力的。如果没有,您是否正在寻找 属性设置器注入 或 Setter 方法注入 ?
请注意,即使您认为它们更漂亮,管理所有这些的最清晰方法就是使用构造函数注入。
或者我遗漏了什么 - 你没有在问题中详细说明你的实际问题。
What is the problem? What is 'problematic' ? Constructor injection is worth the effort. If not, are you looking for Property Setter Injection or Setter Method Injection ?
Beware that even if you think they are prettier, the clearest way to manage all of this is simply by using Constructor Injection.
Or is there something I'm missing - you didn't detail your actual problem in the question.