默认构造函数是否应该将 NULL 传递给另一个构造函数来创建依赖项?

发布于 2025-01-06 18:00:21 字数 352 浏览 1 评论 0原文

我有一堆具有一组依赖项的类。这个项目的依赖注入将是矫枉过正的,所以目前我们在许多情况下有以下模式:

public MyClass() : this(null, null) {}

public MyClass(Dependancy x, Dependancy y)
{
  this.x = x ?? new Dependancy();
  this.y = y ?? new Dependancy();
}

我不喜欢这段代码,但我不完全确定为什么。一个原因是它会干扰传入的参数,另一个原因是我可能希望参数为空,并保持为空。

是否有任何充分的理由避免/使用这种模式或任何其他模式,或者它基本上只是个人偏好?

I have a bunch of classes that have a set of dependencies. Dependency Injection for this project will be overkill, so currently we have the following pattern in a number of cases:

public MyClass() : this(null, null) {}

public MyClass(Dependancy x, Dependancy y)
{
  this.x = x ?? new Dependancy();
  this.y = y ?? new Dependancy();
}

I don't like this code, but I'm not totally sure why. One reason would be the fact that it fiddles with parameters that are passed in, the other is that I might want the parameter to be null, and stay null.

Are there any strong reasons to avoid/use this pattern or any other, or is it basically just personal preference?

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

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

发布评论

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

评论(2

动听の歌 2025-01-13 18:00:21

你不喜欢它有两个原因:

  • 不需要有一个参数化的构造函数来传递 NULL;有第二个无参数构造函数;
  • 与好处相比,拥有第三个类(将其命名为 Factory,将其命名为 Container,都没关系)注入这些默认依赖项永远不会过大。

You do not like it because of two reasons:

  • There is no need to have a parametrized constructor to which you pass NULLs; have a second no-args constructor;
  • Having a third class (name it Factory, name it Container, does not matter) injecting those default dependencies will never be overkill, compared to the benefits.
若有似无的小暗淡 2025-01-13 18:00:21

您发布的代码的问题是您正在使用依赖注入,而您只是没有使用控制反转。 MyClass 已将其依赖项注入其中,但如果它们不符合预期,它会假设控制要执行的操作。有几个原因导致这种情况令人不快:

  1. 较高级别的类 MyClass 与较低级别的类 Dependency 耦合。
  2. MyClass 充当 ServiceLocator,本身就是一种反模式,而且也违反了单一责任原则。
  3. 无参数构造函数有隐藏的副作用;使用它的地方正在不知不觉地构造 MyClass ,其依赖项均设置为 Dependency

正如 @Alessandro Santini 所说,我真的鼓励你放弃这个并使用 DI / IoC 容器。至少摆脱无参数构造函数并强制任何想要构造 MyClass 实例的东西为其提供适当的依赖项。如果给定的依赖项为空,则双参数构造函数应该抛出异常。

The thing with the code you've posted is that you are using dependency injection, you're just not using Inversion of Control. MyClass has its dependencies injected into it, but it then assumes control of what to do if they aren't as it expects. There's a few reasons this is unpleasant:

  1. The higher-level class MyClass is coupled to the lower level class Dependency.
  2. MyClass is acting as a ServiceLocator, which in itself is an anti-pattern, but also violates the Single Responsibility Principle.
  3. The parameterless constructor has hidden side-effects; the places where it's used are unknowingly constructing MyClass with its dependencies both set to Dependency.

As @Alessandro Santini says, I'd really encourage you to scrap this and use a DI / IoC container. At the very least get rid of the parameterless constructor and force anything which wants to construct a MyClass instance to supply it with appropriate dependencies. The two-argument constructor should then throw exceptions if the dependencies it's given are null.

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