gpt4 book ai didi

.net - DI Control-Freak 反模式 : Having trouble understanding

转载 作者:行者123 更新时间:2023-12-02 04:06:26 25 4
gpt4 key购买 nike

我正在阅读 Mark Seemann 所著的《.NET 中的依赖注入(inject)》,但我无论如何也无法理解这一点:

Although the new keyword is a code smell when it comes to VOLATILE DEPENDENCIES, you don't need to worry about using it for STABLE DEPENDENCIES. The new keyword isn't suddenly "illegal" in general, but you should refrain from using it to get instances of VOLATILE DEPENDENCIES.

也许是因为我仍然无法理解环境上下文是一个注入(inject)而不仅仅是一个全局变量,但我只是不明白作者在说什么。

我真的很想从头到尾理解 DI,但现在我陷入了困境,这只是这本书的 1/3...控制狂反模式似乎是每个程序员曾经活过的...

有人有什么见解吗?

最佳答案

波动性(对我来说)是衡量一个类需要更改的可能性的指标。

理想情况下,您设计的类对扩展开放,但对修改封闭(开闭原则)。这并不总是可能的。您接近更改的那些类比其他类波动性更小。

NDepend(.Net 静态分析指标工具)有一个名为 Instability 的指标在我看来这是同义词。他们将其定义为:

Instability (I): The ratio of efferent coupling (Ce) to total coupling. I = Ce / (Ce + Ca). This metric is an indicator of the assembly's resilience to change. The range for this metric is 0 to 1, with I=0 indicating a completely stable assembly and I=1 indicating a completely instable assembly.

您不希望稳定的类依赖于不太稳定的类。

至于决定是否注入(inject),这听起来更像是一个类(Class)角色问题。从领域驱动设计来看,(DDD) 类通常是实体(它们具有身份)、服务(它们编排事物)或值(它们是不可变的且可比较的,如 RED 或 100ml)。

您将注入(inject)服务,您将在值上调用 new 。实体通常来自存储库(您注入(inject)的),但存储库内部会对其调用 new 。这有帮助吗?

关于.net - DI Control-Freak 反模式 : Having trouble understanding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11330877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com