DI控制 - 怪兽反模式:难以理解 [英] DI Control-Freak anti-pattern: Having trouble understanding

查看:244
本文介绍了DI控制 - 怪兽反模式:难以理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在阅读Mark Seemann的.NET中的依赖注入

虽然关键字是一个代码气味,当涉及到VOLATILE
DEPENDENCIES时,您不需要担心使用STABLE
DEPENDENCIES。 新的关键字一般不会突然非法,而是
,但您不应该使用它来获取VOLATILE
DEPENDENCIES的实例。


也许是因为我仍然不能把我的头包裹在环境上下文中,而不是一个全局变量,但是我只是不知道作者是什么说吧。

我真的很想了解DI的顶部到底部,但是现在我被困住了,这只是这本书的1/3。 Control-Freak反模式似乎是每一个曾经活过的程序员...



任何人都有任何见解?

解决方案

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



理想情况下,您可以将类设计为扩展开放式,但不能修改(Open Closed Principle)。这并不总是可能的。你接近改变的那些课程比其他课程更不稳定。



NDepend(a .Net静态分析指标工具)有一个称为不稳定性是同义词。
他们将它们定义为:


不稳定性(I):传出耦合(Ce)与总耦合的比率。 I = Ce /(Ce + Ca)。这个指标是组合弹性变化的指标。该度量的范围为0到1,I = 0表示完全稳定的程序集,I = 1表示完全不稳定的程序集。


你不希望稳定的类依赖于较不稳定的类。



就决定注入而言,这听起来更像一个类角色问题。从域驱动设计,(DDD)类通常是实体(他们有身份),服务(他们编排的东西)或值(它们是不可变的和可比较的,如红色或100ml)。



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


I'm reading Dependency Injection in .NET by Mark Seemann and I can not for the life of me get my head wrapped around this:

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.

Maybe it's because I still can't get my head wrapped around ambient context being an injection instead of just a global variable, but I am just not getting what the author is saying.

I'd really like to understand DI top to bottom but right now I'm stuck and this is just a 1/3 the way through the book... The Control-Freak anti-pattern seems to be every single programmer that ever lived...

Anyone have any insights?

解决方案

Volatility (to me) is a measure of the likeliness that a class will need to be changed.

Ideally you design classes to be open to extension but closed to modification (Open Closed Principle). This isn't always possible. Those classes that you close to change are less volatile than the others.

NDepend (a .Net static analysis metrics tool) has a metric called Instability that in my mind is synonymous. They define this as:

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.

You don't want Stable classes relying on less Stable ones.

As far as deciding to inject or not, that sounds more like a class role issue. From Domain Driven Design, (DDD) Classes are usually either Entities (they have identity), Services (they orchestrate things) or Values (they are immutable and comparable, like RED or 100ml).

You would inject Services, you would call new on Values. Entities usually come from a repository (that you inject), but internally the Repository would call new on them. Does that help?

这篇关于DI控制 - 怪兽反模式:难以理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆