带有DI的.NET Core的静态类或单尔顿的静态类别

发布于 2025-02-11 06:26:17 字数 406 浏览 1 评论 0原文

我正在将公用事业类迁移到.NET核心类库。

起初,我以为我可以简单地为此组成一个静态课程,但是我对 iConfiguration 有一个依赖性,所以我不确定如何继续这样做。

这种情况的最佳实践是什么?

我应该使用 class 并使用构造器注入,然后将其添加到配置中
我应该使用静态类并使用方法注入吗?

或者我什至不应该遇到此类问题: - ?
我想第一个版本更好。
抱歉,如果这是一个愚蠢的问题,我对自己的选择有些困惑。

谢谢你!

I'm migrating a utility class to a .NET Core class library.

At first I thought I can simply make a static class for this but I have a dependency on IConfiguration so I'm not sure how to proceed with this.

What is the best practice for this situation?

Should I use a class and use constructor injection and then add it in ConfigureServices?
Should I use a static class and use method injection?

Or I shouldn't even come across this type of issue :-?
I would guess that the first version is nicer.
Sorry if this is a dumb question, I'm a bit confused for the options I have.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文