身份验证类应该是静态的还是从实例创建?

发布于 12-19 12:34 字数 580 浏览 1 评论 0原文

身份验证类应该是静态的还是从实例创建?

我遇到的问题是,如果不在任何控制器中注入身份验证实例,我就无法访问身份验证,这有点问题。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏尔2024-12-26 12:34:43

这有点问题。

问题是什么?什么是‘有问题’?构造函数注入是值得付出努力的。如果没有,您是否正在寻找 属性设置器注入Setter 方法注入

请注意,即使您认为它们更漂亮,管理所有这些的最清晰方法就是使用构造函数注入。

或者我遗漏了什么 - 你没有在问题中详细说明你的实际问题。

that a bit problematic.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文