简体   繁体   English

更喜欢在接口上嵌入依赖关系,还是将其交给具体的类?

[英]Preferable to embeb dependencies on interfaces or let that job to the concrete classes?

Here is a question that I have to face a lot when doing the design of my little apps. 这是我设计小应用程序时必须面对的一个问题。 Is there a reason I should prefer 有什么理由让我更喜欢

替代文字

over 过度

替代文字

?

If yes, why? 如果是,为什么? If not, are there any specific circunstances where there is a clear advantage in one approach over the other? 如果不是,是否存在任何一种特定方法比另一种方法具有明显优势的特定情况?

I tend to use the first approach, although I am not too sure why. 尽管我不太确定为什么,但我倾向于使用第一种方法。 I guess maybe it is because there may be some concrete implementation of IBonus that might not need a dependency at all, while the second approach is tieing me to something. 我猜可能是因为IBonus的某些具体实现可能根本不需要依赖,而第二种方法却使我束手无策。 A stub/mock class, for example. 例如,存根/模拟类。

Thanks! 谢谢!

It sounds like you understand the subject quite well. 听起来您很了解该主题。 I agree with your argument that there might be an implementation which does not have the dependency and so it's best not to be tied down. 我同意您的观点,即可能存在不具有依赖关系的实现,因此最好不要束缚它。 I typically prefer the first approach, like you, for that very same reason and typically encourage that choice. 由于同样的原因,我通常喜欢第一种方法,就像您一样,并且通常鼓励这种选择。

In general, I would say that if the dependency is an implementation detail, then it should be passed to the constructor of the concrete implementation. 通常,我想说的是,如果依赖项是实现细节,则应将其传递给具体实现的构造函数。 If the parameter is intrinsic to the very definition / nature of the computation, then it is better to pass in the parameter. 如果参数是计算的定义/本质所固有的,那么最好传入参数。 Another situation where it is a good idea to pass the parameter in the interface is where mutation is concerned; 最好在接口中传递参数的另一种情况是涉及突变。 for reentrance, it is better to pass a parameter that you modify over modifying the state of the implementation, so if any side-effects are expected, then I would encourage passing the object to be mutated. 对于重入,最好传递您修改的参数而不是修改实现的状态,因此,如果预期会有任何副作用,那么我鼓励传递要被突变的对象。 That aside, I agree with your view. 除此之外,我同意你的观点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将接口映射到具体类 - Mapping Interfaces to concrete classes 统一通用接口和具体类 - unity generic interfaces and concrete classes 类设计,接口或具体类 - Class design, Interfaces or Concrete classes 公开接口而不是具体的类 - Exposing interfaces instead of concrete classes 当具体类包含其他接口时,如何反序列化接口集合 - How to deserialize collection of interfaces when concrete classes contains other interfaces 为什么我不能将具体类添加到具体类实现的接口列表中? - Why can't I add concrete classes to a list of interfaces which the concrete classes implement? 为什么要为我的具体DataProvider类创建接口 - Why should I create interfaces for my concrete DataProvider classes 使用依赖注入是否需要使用接口而不是具体类? - Does the use of Dependency Injection necessitate the use of Interfaces to be injected rather than concrete classes? 我可以让实体框架使用具体类而不是接口(用于Web服务序列化) - Can I get the entity framework to use concrete classes instead of interfaces (for web service serialisation) 使用将接口而不是具体类传递给@ChildContent 的 [CascadingParameter]<cascadingvalue> 在 blazor</cascadingvalue> - Passing interfaces instead of concrete classes into @ChildContent's [CascadingParameter] using <CascadingValue> in blazor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM