DI应该在哪里(层)使用?

发布于 2024-09-03 19:48:43 字数 165 浏览 6 评论 0原文

是否有任何层使用 DI 是不好的做法?在上一个问题中,一位用户提到 DI 应该只用在UI层。

Is there any layer where it is bad practice to use DI? In a previous question a user mentioned that DI should only be used in the UI layer.

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

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

发布评论

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

评论(4

难理解 2024-09-10 19:48:43

我认为您可能会误解 Daren Dimitrov 提供的答案。除非我弄错了,否则引发这个问题的那句话是:“此外,您不应该在应用程序的业务层中创建与 DI 框架的依赖关系。”

我相信他所说的是所有依赖信息/映射都应该在应用程序的最高级别创建。这并不是说依赖关系不会存在于应用程序的所有层中,只是较低级别不负责设置映射。

如果我的解释是正确的,那么我同意他的观点——在应用程序的表面设置依赖关系映射。相反,如果他说你永远不应该解决较低层的依赖关系,那么我将不得不不同意。

I think that you might be misinterpreting the answer provided by Daren Dimitrov. Unless I'm mistaken, the line that spawned this question is: "Also you shouldn't be creating a dependency with the DI framework in the business layers of the application."

I believe what he is saying is that all dependency information/mappings should be be created at the highest level of an application. That is not to say that dependencies will not be present throughout all tiers of your application, just that the lower levels are not responsible for setting up the mappings.

If my interpretation is correct then I agree with him - set up your dependency mappings at the surface of your application. If instead he is saying that you should never resolve a dependency in the lower layers then I would have to disagree.

瑕疵 2024-09-10 19:48:43

依赖注入 (DI) 应该在任何地方使用。但是,请注意,DI 只是一组模式,而不是框架或库。考虑 DI 的最佳方式是,它是由构造函数注入启用的松散耦合代码。您可以一直编写 DI 友好的代码,而无需引用 DI 容器 或使用服务定位器反模式

那么,我想到的下一个问题是组件应该在哪里组成?这可能就是原始声明的含义:在最外层编写应用程序 - UI 或(Web)服务接口。这就是我所说的组合根。它使其余代码保持模块化和灵活性。

在组合根中,您可以使用Poor Man's DI或适当的DI Container。我强烈建议您使用 DI 容器,但这不是必需的。

为了说明 DI 的含义,这里有一个示例:

public class Foo : IFoo
{
    private readonly IBar bar;

    public Foo(IBar bar)
    {
        if (bar == null)
        {
            throw new ArgumentNullException("bar");
        }

        this.bar = bar;
    }

    public IBaz Baz(ISnafu snafu)
    {
        return this.bar.Snafize(snafu).ToBaz();
    }
}

IBaz 实现可以在与定义 Foo 类的库完全独立的库中实现。此外,堆栈中较高层的消费者不需要了解有关 Foo 和 IBar 的任何信息 - 他们可以只使用 IFoo:

public class MyClass : ISomeInterface
{
    private readonly IFoo foo;

    public MyClass(IFoo foo)
    {
        if (foo == null)
        {
            throw new ArgumentNullException("foo");
        }

        this.foo = foo;
    }

    // ...
}

这提供了松散耦合,这是 DI 的动机。根据需要重复多次。

Dependency Injection (DI) should be used everywhere. However, notice that DI is only a set of patterns - not a framework or a library. The best way to think about DI is that it is loosely coupled code enabled by Constructor Injection. You can write DI-friendly code all the way without ever referencing a DI Container or using the Service Locator anti-pattern.

The next question that comes to mind, then, is where should components be composed? This is probably what is meant by the original statement: only compose the application in the outermost layer - UI or (web) service interface. This is what I call the Composition Root. It keeps the rest of the code modularized and flexible.

At the Composition Root, you can use Poor Man's DI or a proper DI Container. I stronly suggest that you use a DI Container, but it's not required.

To illustrate what I mean by DI, here's an example:

public class Foo : IFoo
{
    private readonly IBar bar;

    public Foo(IBar bar)
    {
        if (bar == null)
        {
            throw new ArgumentNullException("bar");
        }

        this.bar = bar;
    }

    public IBaz Baz(ISnafu snafu)
    {
        return this.bar.Snafize(snafu).ToBaz();
    }
}

The IBaz implementation could be implemented in a completely seperate library from the one defining the Foo class. Furthermore, consumers higher in the stack need not know anything about Foo and IBar - they can just consume IFoo:

public class MyClass : ISomeInterface
{
    private readonly IFoo foo;

    public MyClass(IFoo foo)
    {
        if (foo == null)
        {
            throw new ArgumentNullException("foo");
        }

        this.foo = foo;
    }

    // ...
}

This provides the loose coupling that is the motivation for DI. Repeat as much as necessary.

落花浅忆 2024-09-10 19:48:43

DI 应该用在一个组件依赖于另一个组件的任何情况下,无论是 UI --> 还是 UI。商业,商业——>数据访问或其他什么。我不认为它应该只用于 UI,只是 DI 使测试 UI 变得更容易,因此似乎有更多直接的好处。

DI should be used in any case where one component is dependent on another, whether it's UI --> Business, Business --> Data Access or whatever. I don't think it should only be used for UI, it's just that DI makes testing the UI easier, and so appears to have more of an immediate benefit.

初见终念 2024-09-10 19:48:43

我不同意这种说法。您引用的上一个问题要么不正确,要么您误解了它。

DI当然不仅仅存在于UI层。我只会同意这一点,前提是控制器是视图层的一部分。

只要可以正确提供依赖项,就可以使用 DI。

无论您选择使用工厂还是 DI 解决方案,都是您的选择。

I disagree with that statement. Either the previous question you cite is incorrect or you misinterpreted it.

DI is certainly not only in the UI layer. I would only agree with that on the condition that controllers are part of the view layer.

You use DI wherever dependencies can be properly provided.

Whether you choose to use a factory or a DI solution is your choice.

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