使类依赖项出现在接口中而不是属性

发布于 2025-01-08 04:31:35 字数 431 浏览 0 评论 0原文

我有一个接口和一个实现该接口的类。

我要求该类依赖于 ITimer。创建私有成员并传入构造函数参数,并将私有成员分配给该构造函数参数。

如何使接口以某种方式声明此 ITimer 但不作为属性?这并不是说我无法正确测试 MyClass,但我认为接口应该是类必须遵守的合同,因此我的类具有依赖项,那么我如何使其出现在接口中?

public interface IMyInterface
{
  void DoSomething();
}

public class MyClass : IMyInterface
{
  private ITimer MyTimer;

  public MyClass(ITimer timer)
  {
    MyTimer = timer;
  }

  public void DoSomething()
  {

  }
}

I have an interface and a class that implements that interface.

I have a requirement that the class has a dependency on ITimer. A private member is created and a constructor argument is passed in and the private member is assigned to that constructor parameter.

How do I make it so that the interface has this ITimer declared somehow but not as a property? Its not as if I can't test MyClass properly but I thought the interface was supposed to be a contract that the class must adhere to therefore my class has a dependency so how do I make this appear in the interface?

public interface IMyInterface
{
  void DoSomething();
}

public class MyClass : IMyInterface
{
  private ITimer MyTimer;

  public MyClass(ITimer timer)
  {
    MyTimer = timer;
  }

  public void DoSomething()
  {

  }
}

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

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

发布评论

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

评论(6

找回味觉 2025-01-15 04:31:35

这无法通过 C# 中的接口来完成。您可以做的是从抽象类继承而不是接口。

public abstract class AMyClass
{
  protected ITimer MyTimer;

  public AMyClass(ITimer timer)
  {
    MyTimer = timer;
  }

  public abstract void DoSomething();
}

public class MyClass : AMyClass
{
  public override void DoSomething()
  {

  }
}

This cannot be done with an interface in C#. What you could do is to inherit from an abstract class instread of an interface.

public abstract class AMyClass
{
  protected ITimer MyTimer;

  public AMyClass(ITimer timer)
  {
    MyTimer = timer;
  }

  public abstract void DoSomething();
}

public class MyClass : AMyClass
{
  public override void DoSomething()
  {

  }
}
ˉ厌 2025-01-15 04:31:35

这不会起作用,但它不会起作用是件好事:

您对 ITimer 的依赖不是接口的一部分,而是实现的一部分 的界面 - 所以将其隐藏在界面中是正确的事情。

如果接口的所有实现都需要依赖于ITimer(并且如果是这样),那么应该附加您的接口声明来解决这个问题。

This will not work, and it is good, that it will not work:

Your dependency on ITimer is not part of the interface, it is part of your implementation of the interface - so having this hidden from the interface is the right thing.

If ALL implementations of the interface need a dependency on ITimer (and only if so), then your interface declaration should be appended to care for that.

余生共白头 2025-01-15 04:31:35

为什么财产不合适?您可以拥有一个带有私人设置器的财产,它似乎完全可以满足您的需要。

另一种选择是从具有受保护的 Timer 的抽象基类派生并同时实现接口。

理论上,依赖注入就是您所寻找的。因此,也许值得花时间看看控制反转容器。

Why is property not appropriate? You can have a property with a private setter which does exactly what you need, it seems.

Other option is to derive both from an abstract base class with protected Timer and implement the interface at the same time.

Theoretically Dependency Injection is what you are looking for. So may be having a look at Inversion of Control containers might be worth the time.

神回复 2025-01-15 04:31:35

如果您明确不想在接口上有诸如 ITimer 之类的依赖项,那么它听起来像是一个实现细节,因此它不是正确定义为合同的一部分。一种方法是“忘记ITimer;只有某些实现可能需要它,这是 IoC/DI 解析器所关心的”。

显然,构造函数不是接口的一部分。我唯一能想到的另一件事(同时保持 IMyInterface 远离 API)是拥有一个 IMyInterfaceFactory 接口,以及一个 Create 方法,该方法采用ITimer - 但这会强制执行一组特定依赖项,这可能无法反映实际的实现。

如果上述套件都没有,并且ITimer本质上是界面的一部分,那么我将使其正式成为界面的一部分。

If you have a dependency such as ITimer that you explicitly don't want on the interface, then it sounds like an implementation detail, so it is correct that it isn't defined as part of the contract. One approach is "forget about ITimer; only some implementations might need it, and that is the concern of an IoC/DI resolver".

Constructors are not part of the interface, obviously. The only other thing I can think of (while keeping IMyInterface off the API) is to have an IMyInterfaceFactory interface, with a Create method that takes an ITimer - but that is then forcing a particular set of dependencies, which may not reflect the actual implementations.

If none of the above suite, and ITimer is essentially part of the interface, I'd make it formally part of the interface.

分开我的手 2025-01-15 04:31:35

接口是类公开的功能的契约。如果接口中没有自然放置 ITimer 的位置,那么它看起来就像是实现它的特定类中的实现细节,因此应该保留在该类中。

An interface is a contract to a capability that a class exposes. If there's no natural place for ITimer in the interface, then it looks like an implementation detail in a specific class that implements it, and as such, should remain in the class.

还给你自由 2025-01-15 04:31:35

如果一个类可以在不使用ITimer的情况下实现您的接口,那么它是否不遵守该接口?显然确实如此。

ITimer 的使用是一个实现细节,因此不应在接口中表示。

If a class can implement your interface without using ITimer, does it not adhere to the interface? It obviously does.

The use of ITimer is an implementation detail, and so it shouldn't be represented in the interface.

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