1

I'm making a console application that returns a lot of information directly to the console. I do this with a custom logger. Actually my logger implementation is just a frontend to print (with some extras).

The application is quite complex and I made it in DDD style, but one thing doesn't work for me - almost every business class that does something, I pass an instance of Logger through DI. First of all, this is very cumbersome, and secondly it obscures the code. Business classes have a Logger dependency that might as well be turned off, because if the action executes correctly, it doesn't return anything anyway.

The question is - do you pass the logger instance to your business classes explicitly via DI or do you have a global logger object that you use directly in the code? Or any other way? Maybe you have some rule of thumb for when DI and when global?

etroby
  • 19

1 Answers1

1

The safe default is to never use globals.

Sometimes, after finishing something it turns out that a global (aka singleton) would have sufficed, of course. And yes, passing dependencies seems like a chore. But the costs of always using DI are small, in comparison to the benefits.

fdreger
  • 266