Question

What is the convention when I have dependencies which are optional, like for example logging ? I want to have them injected by properties. And what after when nobody set it? Check for null every time ? Or get a fake logger which does nothing ? 1.The problem is not related only for logging. 2. I am asking for convention when injecting via properties.

Was it helpful?

Solution

Property injection is typically used when you can generally rely on a default implementation but want the option of injecting an alternative.

In your example, you might create a DefaultLogger that the class uses unless someone passes in an alternative via a property.

I think there should always be an instance of something, a 'null object' maybe. Checking for null doesn't feel right.

Also, have you considered decoration? Logging is a great example of how decoration can add extra, optional, features.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top