DI Control-Freak anti-pattern: problems with understanding

I read Mark Seemann's Injection Dependency in .NET, and I can’t trick me for the rest of my life:

Although the keyword new is the smell of code, when it comes to VOLATILE DEPENDENCE, you do not need to worry about using it for STABLE DEPENDENCE. the new keyword is not suddenly "illegal" at all, but you should refrain from using it to get instances of VOLATILE DEPENDENCE.

Maybe because I still can’t get my head to wrap myself around the surrounding context, being an injection, not just a global variable, but I just don’t understand what the author is saying.

I would really like to deal with DI from top to bottom, but now I'm stuck, and this is only 1/3 of the way through the book ... The Control-Freak anti-pattern seems to be every programmer who ever lived ...

Does anyone have any ideas?

+6
source share
3 answers

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

Ideally, you create classes for extension, but are closed for modification (open closing principle). This is not always possible. Those classes that you are close to change are less volatile than others.

NDepend (a static analysis metric tool.) Has a metric called Instability , which, in my opinion, is synonymous. They define it as:

Instability (I): ratio of efferent bond (Ce) to total bond. i = Ce / (Ce + Ca). This metric is an indicator of assembly resilience to change. The range for this metric is from 0 to 1, while i = 0 indicates a fully stable assembly, and i = 1 indicates a completely unstable assembly.

You do not want Stable classes to rely on less stable ones.

As for making a decision or not, this is more like a problem with the role of the class. Of the Domain Driven Design (DDD) classes, they are usually either entities (they have an identity), or services (they organize things), or values ​​(they are immutable and comparable, for example, RED or 100 ml).

You will enter Services, you will call the new value "Values". Objects usually come from the repository (which you enter), but internally the repository will call them new. Does it help?

+7
source

Typically, a DI container resolves all dependencies for a particular implementation and manages the lifetime for you. But DI Control-Freak does the opposite: it creates a specific object using the new keyword, enters them and processes only the lifetime. Thus, no one else has the ability to customize and handle dependencies outside of your code. And thus, reuse or testing, providing other implementations or fakes.

Here is a quote from @ mark-seemann book that answers your question:

I called it CONTROL FREAK to describe a class that will not give up control of its DEPENDENCES.

This happens every time we create a new instance of the type using a new key word. When we do this, we explicitly declare that we will control the lifetime of the instance and that no one else will be able to intercept this particular object.

+1
source

I had not heard these terms before, but, conveying what I know about dependency injection, I would define them as follows.

A strong dependency is one whose specific implementation will not change in a class or configuration over time or in different situations.

An unstable dependency is one whose particular implementation may change in a class or configuration over time or in different situations.

If you can forgive the example in Java, the stable dependency would be StringBuilder . If you are writing a method to create a string, you do not need to enter a StringBuilder , you can just create it. The same goes for ArrayList , HashMap , etc. Apart from the standard library classes, if you are writing a banking application, you probably will not introduce RunningTotaliser , because it is an object that simply adds numbers for you.

Examples of volatile dependencies in the standard library are harder to come up with. A classic example would be a DataSource , an object that encapsulates connection data for a database, and this will almost certainly not be provided by the code that actually creates the connections.

+1
source

Source: https://habr.com/ru/post/919681/


All Articles