DEV Community

Cover image for Developer diary #3. Injection in constructor
Kiolk
Kiolk

Posted on

Developer diary #3. Injection in constructor

Today, I again made sure that injection in the constructor is the best way to pass dependencies to class. If you do it another way, you can get problems when you write tests. Injection into the constructor allowed you to very simply mock any dependencies. 

Sometimes, it is very desirable to use the static method of some utility class, but it turns into a headache when you start writing the tests. You will try to solve this problem by using some libraries. But it is a wrong way. A more efficient way is to wrap this class and pass through the constructor.

Constructor is the gate for dependencies. 

Top comments (0)